Javascript Construction category
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
- / / 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
- function User () (
- this. username = "zhangsan"; / / this. We must not forget
- this. say = function (){// this. We must not forget
- alert ( "username:" + this. username); / / this. We must not forget
- )
- / / The following notes written 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, 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
- / / Multiple instances do not share the type of object attribute values, as follows:
- function User () (
- this. username = new Array ();// this. We must not forget
- this. say = function (){// this. We must not forget
- alert ( "username:" + this. username); / / this. We must not forget
- )
- )
- var user = new User ();
- user.username.push ( "zhangsan");
- user.say ();// username: zhangsan
- var user1 = new User ();
- 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
- / / 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
- 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
//原型方式定义类,缺点:类的属性值如果是引用类型的(非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
- / / The attribute value type is invoked if the type of (non-Number and String type), then multiple instances of object sharing, 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 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
- / / 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.
- function User () (
- this. username = "zhangsan";
- )
- User.prototype.say = function () (
- alert ( "username:" + this. username);
- )
- var user = new User ();
- 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
- / / / / 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
- 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 ();// not print execute ..., then create a method only, that is, methods will only produce a
- 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
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/





















