CodeWeblog.com » java code,attribute,prototype » Javascript Construction category

Javascript Construction category

This article is reproduced in: http://ttitfly.javaeye.com/blog/193663





Construction of javascript in 4 major ways
1. Tectonic defined category
2. Prototype defined category
3. A combination of structure and creating a prototype category
4. Prototype dynamic way

Have their own advantages and disadvantages, as follows
1. Tectonic defined categories, advantages: multiple instances do not share the type of object attribute values, disadvantages: Each instance of the object function will have a say

Java code

  1. / / Structure defined categories advantages: multiple instances do not share the type of object attribute values, disadvantages: Each instance of the object function will have a say
  2. function User () (
  3. this. username = "zhangsan"; / / this. We must not forget
  4. this. say = function (){// this. We must not forget
  5. alert ( "username:" + this. username); / / this. We must not forget
  6. )
  7. / / The following notes written in this way does not
  8. / / Function say () (
  9. / / Alert ( "username:" + this.username);
  10. / /)
  11. )
  12. var user = new User ();
  13. user.username = "lisi";
  14. user.say ();// username: lisi
  15. var user1 = new User ();
  16. user1.say ();// username: zhangsan, from the impact of user object

//构造方式定义类,优点:多个实例对象不共享类的属性值,缺点:每个实例对象都会产生出一个函数say
function User(){
	this.username = "zhangsan";//this.不能丢
	
	this.say = function(){//this.不能丢
		alert("username:" + this.username );//this.不能丢
	}
	//下面注释的这种写法不对
//	function say(){
//		alert("username:" + this.username );
//	}
}

var user = new User();
user.username = "lisi";
user.say();//username:lisi

var user1 = new User();
user1.say();//username:zhangsan,不受user对象的影响


/ / Multiple instances do not share the type of object attribute values:

Java code

  1. / / Multiple instances do not share the type of object attribute values, as follows:
  2. function User () (
  3. this. username = new Array ();// this. We must not forget
  4. this. say = function (){// this. We must not forget
  5. alert ( "username:" + this. username); / / this. We must not forget
  6. )
  7. )
  8. var user = new User ();
  9. user.username.push ( "zhangsan");
  10. user.say ();// username: zhangsan
  11. var user1 = new User ();
  12. user1.say ();// user1 the username is empty, not zhangsan, because the attribute values from user1 affected user

//多个实例对象不共享类的属性值,如下:
function User(){
	this.username = new Array();//this.不能丢
	
	this.say = function(){//this.不能丢
		alert("username:" + this.username );//this.不能丢
	}
}

var user = new User();
user.username.push("zhangsan");
user.say();//username:zhangsan

var user1 = new User();
user1.say();//user1的username为空,不为zhangsan,因为user1的属性值不受user影响


2. Prototype defined categories, Disadvantages: the attribute value type is invoked if the type of (non-Number and String type), then multiple instances of shared object

Java code

  1. / / Prototype defined categories, Disadvantages: the attribute value type is invoked if the type of (non-Number and String type), then multiple instances of shared object
  2. function User () (
  3. )
  4. User.prototype.username = "zhangsan";
  5. User.prototype.say = function () (
  6. alert ( "username:" + this. username);
  7. )
  8. var user = new User ();
  9. user.username = "lisi";
  10. user.say ();// username: lisi
  11. var user1 = new User ();
  12. user1.say ();// username: zhangsan

//原型方式定义类,缺点:类的属性值如果是引用类型的(非Number和String类型),则多个实例对象共享
function User(){
}
User.prototype.username = "zhangsan";
User.prototype.say = function(){
	alert("username: " + this.username );
}

var user = new User();
user.username = "lisi";
user.say();//username:lisi

var user1 = new User();
user1.say();//username:zhangsan


If the attribute value type is a reference type (non-Number and String type), then multiple instances of shared object:

Java code

  1. / / The attribute value type is invoked if the type of (non-Number and String type), then multiple instances of object sharing, the following
  2. function User () (
  3. )
  4. User.prototype.username = new Array ();
  5. User.prototype.say = function () (
  6. alert ( "username:" + this. username);
  7. )
  8. var user = new User ();
  9. user.username.push ( "zhangsan");
  10. user.say ();// username: zhangsan
  11. var user1 = new User ();
  12. user1.say ();// username: zhangsan, because user1 attributes user impact will be, user1 and user point to the same reference, that is, share the same attributes

//类的属性值如果是引用类型的(非Number和String类型),则多个实例对象共享,如下
function User(){
}
User.prototype.username = new Array();
User.prototype.say = function(){
	alert("username: " + this.username );
}

var user = new User();
user.username.push("zhangsan") ;
user.say();//username:zhangsan

var user1 = new User();
user1.say();//username:zhangsan,因为user1属性也会受到user的影响,user1和user指向同一引用,即共享同一属性


3. A combination of structure and creating a prototype categories: advantages: the method will only have a category one, will not produce a large number of methods, attributes not shared at the same time; shortcomings: the definition of separation of properties and methods are not too good.

Java code

  1. / / Structure and prototype to create a combination of categories: advantages: the method will only have a category one, will not produce a large number of methods, attributes not shared at the same time; shortcomings: the definition of separation of properties and methods are not too good.
  2. function User () (
  3. this. username = "zhangsan";
  4. )
  5. User.prototype.say = function () (
  6. alert ( "username:" + this. username);
  7. )
  8. var user = new User ();
  9. alert (user.username);

//构造和原型结合方式创建类:优点:类的方法只会产生一个,不会产生大量方法,同时属性还不共享;缺点:属性和方法分开定义不是太好。
function User(){
	this.username = "zhangsan";
}
User.prototype.say = function(){
	alert("username: " + this.username );
}
var user = new User();
alert(user.username);


4. Dynamic prototype method: advantages: the method will only have a category one, will not produce a large number of methods, not yet at the same time share properties, attributes and methods at the same time is not separated from the definition of

Java code

  1. / / / / Dynamic prototype method: advantages: the method will only have a category one, will not produce a large number of methods, not yet at the same time share properties, attributes and methods at the same time is not separated from the definition of
  2. function User () (
  3. this. username = "zhangsan";
  4. if (typeof User.flag == "undefined") (
  5. alert ( "execute ...");
  6. User.prototype.say = function () (
  7. alert ( "username:" + this. username);
  8. )
  9. User.flag = true;
  10. )
  11. )
  12. var user1 = new User ();// execute ...
  13. var user2 = new User ();// not print execute ..., then create a method only, that is, methods will only produce a
  14. user1.say ();// username

////动态的原型方式:优点:类的方法只会产生一个,不会产生大量方法,同时属性还不共享,同时属性和方法不是分开定义的

function User(){
	this.username = "zhangsan";
	
	if(typeof User.flag == "undefined"){
		alert("execute...");
		User.prototype.say = function(){
			alert("username: " + this.username );
		}
		
		User.flag = true;
	}
}

var user1 = new User();//execute...
var user2 = new User();//不会打印出execute...,则说明方法只创建了一次,即方法只会产生一个
user1.say();//username


Summary:
Structure defined categories: shortcomings: the method of class, each instance will have a target, resulting in a large number of methods; advantages: all instances of objects have a separate class of property, that property is not shared definition of the prototype method categories: Disadvantages: all instances of the object are co-owners of the properties of a class, that is, shared attributes. Advantages: the method will only have a category one, a large number of methods will not produce a combination of structure and creating a prototype categories: advantages: the method will only have a category one, will not produce a large number of methods, attributes not shared at the same time; Disadvantages: properties and not a very good way to separate the definition.
Dynamic prototype method: advantages: the method will only have a category one, will not produce a large number of methods, not yet at the same time share properties, attributes and methods at the same time is not separated from the definition of





Digg Technorati StumbleUpon Mixx del.icio.us Reddit BlinkList Furl YahooMyWeb feedburner

Tags: java code (RSS), attribute (RSS), prototype (RSS), array (RSS), instances (RSS), advantages and disadvantages (RSS), code structure (RSS), tectonic (RSS), lisi (RSS)

Permalink: http://www.codeweblog.com/javascript-construction-category/

Leave a reply