First mode: Factory mode
- var lev=function () {
- return "Ah dozen";
- };
- function Parent () {
- var child = new Object ();
- Child.name="Bruce Lee";
- child.age="30";
- Child.lev=lev;
- return 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 to define the property as a method's property outside of the function, which avoids the need to create the method repeatedly
2. When referencing this object, the use of var x = parent () instead of var x = new parent (), because the latter can be a lot of problems (the former is also a factory classic, the latter is called the Hybrid Factory mode), it is not recommended to use the new method of the object
3. Return the object at the end of the function
4. It is not recommended to create objects this way, but you should understand
Second mode: constructor mode
- var lev=function () {
- return "Ah dozen";
- };
- function Parent () {
- this.name="Bruce Lee";
- this.age="30";
- This.lev=lev;
- };
- var x =new Parent ();
- alert (x.name);
- Alert (X.lev ());
Description
1. Use the constructor method to create an object in comparison to the Factory mode, without having to re-create the object inside the function, and use this as the reference, and the function does not need to explicitly return
2. As with Factory mode, although the value of a property can be a method, throwing suggests that the method be defined outside the function
3: Similarly, it is not recommended to create objects this way, but you still need to know
3rd Mode: Prototype mode
- var lev=function () {
- return "Ah dozen";
- };
- 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. Properties are not defined in the function
2. Defining a property with the prototype attribute
3. Similarly, it is not recommended to create objects in such a way
Fourth mode: Mixed constructors, prototype mode (recommended)
- function Parent () {
- this.name="Bruce Lee";
- this.age=32;
- };
- parent.prototype.lev=function () {
- return this.name;
- };;
- var x =new Parent ();
- Alert (X.lev ());
Description: 1. This pattern refers to the mix and match use of the constructor and prototype mode
2. Define all properties that are not methods in the function (constructor mode)
Use prototype of all property values as methods to define outside of a function (prototype mode)
3. It is recommended to create an object in such a way that it is beneficial and why it is not used alone in the way of constructors and prototypes, as space issues are not discussed here
Fifth mode: Dynamic prototyping mode
- function Parent () {
- this.name="Bruce Lee";
- this.age=32;
- ;
- 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 prototype mode
2. In this mode, attributes are defined directly in the function for the properties of the method, but because
- 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 repeatedly created
3. It is recommended to use this mode