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

// Structured to define the class, the benefits  : More than one instance of the object does not share the class properties value weaknesses  : Each instance objects can have a function say  
function User(){
	this.username = "zhangsan";//this. Can't leave  
	
	this.say = function(){//this. Can't leave  
		alert("username:" + this.username );//this. Can't leave  
	}
	// The following comment in this way does not  
//	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, Not affected by the impact of user object  


/ / 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

// More than one instance of the object does not share the class properties value  :
function User(){
	this.username = new Array();//this. Can't leave  
	
	this.say = function(){//this. Can't leave  
		alert("username:" + this.username );//this. Can't leave  
	}
}

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

var user1 = new User();
user1.say();//user1 The username is an empty, not to zhangsan  , Because user1 property values are not affected by user impact  


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

// Prototypes define class  , Disadvantages  : The class attribute value if this is a reference type  ( Non-Number, and string types  ) , Multiple instance objects share  
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

// The class attribute value if this is a reference type  ( Non-Number, and string types  ) , Multiple instance objects share  , The following  
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, Because user1 properties will also be affected by the user, user user1 and point to the same reference, which share the same properties  


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);

// Structure and prototype combination modes to create class  : Advantages  : The method of the class will only produce one, do not generate large amounts of method  , At the same time property is also not shared  ; Disadvantages  : Properties and methods defined separately is not too good.  
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

//// Dynamic way of prototype  : Advantages  : The method of the class will only produce one, do not generate large amounts of method  , At the same time property is also not shared  , At the same properties and methods are not defined separately  

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();// Do not print out execute  ... Then the method to create only once, that is, the method would only provide a  
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

  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of Javascript Construction category

  • JS after ajax request return of the problem can not be implemented

    1: Send ajax request, in the onComplete, if back when the html contains a javascript, then these javascrip and will not be realized, it does not mean not to implement. This problem has troubled me for a long time, behind the hair and then put this kn ...

  • Javascript Object Model

    Javascript Object Model

  • Hibernate II Study Notes

    11. Many-to-many Of many that can be converted to two one-to-many many-to-many data only from one end of the maintenance, if at both ends of maintenance, will be reported to the conflict between form the primary key of the error message. Many-to-many quer

  • JS in the Date type conversion

    http://blog.csdn.net/yzy0612/archive/2007/08/07/1730732.aspx

  • JavaScript inheritance

    About JavaScript inherited a small example ... <! DOCTYPE HTML PUBLIC "- / / W3C / / DTD HTML 4.01 / / EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" conte ...

  • Hibernate Inteceptor

    The end of the project stage, the client suddenly put forward a very troublesome but normal demand, the system records all changes must be carried out. Formats such as: 2004.1.1 12:30 Ikuya wind orders Sales Order Date 2004.1.2-> 2004.1.3 The firs ...

  • jboss ejb3 Message Driven Bean

    Super Medium ejb hate. . . . . . . . . . . . . . . . . . . ================================================ To configure a Message Driven Bean in a different application server parameters are not the same. Currently only passed the test jboss. Messag ...

  • FLEX: integrating Spring + Hibernate

    Before a friend also wanted to study development of FLEX. Asked me to help him to be a small sample. Spent a weekend time, to integrate a sampleproject. Client: FLEX Server: Spring2.5 + Hibernate3.2 + Hibernate-annotations3.3.1 + MySQL5 FDS: BlazeDS3 ...

  • The level Hibernate cache

    Hibernate cache level: (1) a cache is very short and the session life cycle consistent, also known as session-level cache-level cache or transaction-level cache (2) Ways of Supporting level cache: get (); load (); iterator (); only entity object cach ...

Leave a Reply

Recent
Recent Entries
Tag Cloud
Random Entries