The first model: Factory way
Copy Code code as follows:
var lev=function () {
Return to "cloud-dwelling community";
};
function Parent () {
var child = new Object ();
Child.name= "Script";
Child.age= "4";
Child.lev=lev;
return to child;
};
var x = Parent ();
alert (x.name);
Alert (X.lev ());
Description
1. Define an object in a function and define various properties of the object, although the property can be a method, but it is recommended that the property be defined as a property of the method outside the function, so that the method cannot be repeatedly created
2. When referencing the object, it uses var x = parent () instead of var x = new parent (), because the latter can have many problems (the former is also a factory classic, the latter is called a hybrid factory approach), and it is not recommended to use the object in a new way
3. Returns the object at the end of the function
4. It is not recommended to create objects in this way, but you should understand
Second Pattern: constructor method
Copy Code code as follows:
var lev=function () {
Return to "cloud-dwelling community";
};
function Parent () {
This.name= "Script";
This.age= "30";
This.lev=lev;
};
var x =new Parent ();
alert (x.name);
Alert (X.lev ());
Description
1. Use constructors to create objects in comparison to the factory approach, without having to recreate the object within the function, and use this to refer to, and the function does not need to return explicitly
2. As with Factory mode, although the value of the attribute can be a method, it is recommended that the method be defined outside the function
3. Similarly, it is not recommended to create objects in this way, but you still need to understand
Model Three: Prototype mode
Copy Code code as follows:
var lev=function () {
Return to "cloud-dwelling community";
};
function Parent () {
};
Parent.prototype.name= "Bruce Lee";
Parent.prototype.age= "30";
Parent.prototype.lev=lev;
var x =new Parent ();
alert (x.name);
Alert (X.lev ());
Description
1. Do not define attributes in functions
2. Use the prototype property to define a property
3. Similarly, it is not recommended to create objects in such a way
mode Fourth: Hybrid constructors, prototyping methods (recommended)
Copy Code code as follows:
function Parent () {
This.name= "Script";
this.age=4;
};
Parent.prototype.lev=function () {
return this.name;
};;
var x =new Parent ();
Alert (X.lev ());
Description: 1. This pattern refers to the blending and collocation using the constructor method and the prototype method.
2. Define a property that is not a method in a function (constructor method)
Use prototype to define the properties of all property values as methods (prototype method)
3. It is recommended that you create objects in such a way that it is beneficial and why you do not use the constructor method and the prototype method alone, due to the space problem is not discussed here
mode Fifth: Dynamic prototyping mode
Copy Code code as follows:
function Parent () {
This.name= "Script";
this.age=4;
if (typeof parent._lev== "undefined") {
Parent.prototype.lev=function () {
return this.name;
}
Parent._lev=true;
}
};
var x =new Parent ();
Alert (X.lev ());
Description
1. Dynamic prototyping can be understood as a hybrid constructor, a special case of the prototype approach
2. In this pattern, properties are defined directly in the function as the properties of the method, but because
Copy Code code as follows:
if (typeof parent._lev== "undefined") {
Parent._lev=true;}
This ensures that when an instance of the object is created, the method of the property is not created repeatedly
3. It is recommended to use this model