The first mode: js factory mode
var lev=function(){ return "Ah, beat"; }; function Parent(){ var Child = new Object(); ="Bruce Lee"; ="30"; =lev; return Child; }; var x = Parent(); alert(); alert(());
illustrate:
1. Define the object in the function and define various properties of the object. Although the properties can be methods, it is recommended to define the properties of the properties as methods outside the function, so as to avoid repeated creation of the method.
2. When referencing this object, var x = Parent() is used instead of var x = new Parent(); because the latter may have many problems (the former has also become a classic factory method, and the latter is called a hybrid factory method). It is not recommended to use the new method to use this object.
3. Return the object at the end of the function
4. This method is not recommended to create objects, but you should understand
The second mode: js constructor mode
var lev=function(){ return "Ah, beat"; }; function Parent(){ ="Bruce Lee"; ="30"; =lev; }; var x =new Parent(); alert(); alert(());
illustrate:
1. Compared with the factory method, using the constructor method to create objects, there is no need to rebuild the function internally, but use this reference, and the function does not need to explicitly return it.
2. Like the factory mode, although the value of the property can be a method, it is recommended to define the method outside the function.
3.. Similarly, it is not recommended to use this method to create objects, but you still need to understand
The third mode: js prototype mode
var lev=function(){ return "Ah, beat"; }; function Parent(){ }; ="Bruce Lee"; ="30"; =lev; var x =new Parent(); alert(); alert(());
illustrate:
1. The attributes are not defined in the function
2. Use prototype attribute to define attributes
3. Similarly, it is not recommended to use this method to create objects.
The fourth mode: a mixed pattern of constructor + prototype js (recommended)
function Parent(){ ="Bruce Lee"; =32; }; =function(){ return ; }; var x =new Parent(); alert(());
illustrate:
1. This pattern refers to the use of constructor and prototype methods in a mix and match.
2. Define all attributes whose properties are not methods in a function (constructor method)
Define all attributes with values of methods using prototype outside the function (prototype)
3. It is recommended to use this method to create objects. This is beneficial and why not use the constructor method and prototype method alone. Due to space issues, it will not be discussed here.
The fifth mode: dynamic prototype mode of constructor + prototype (recommended)
function Parent(){ ="Bruce Lee"; =32; if(typeof Parent._lev=="undefined"){ =function(){ return ; } Parent._lev=true; } }; var x =new Parent(); alert(());
illustrate:
1. Dynamic prototype method can be understood as a hybrid constructor, a special example of the prototype method
2. In this pattern, the attributes whose attributes are methods are directly defined in the function, but because
if(typeof Parent._lev=="undefined"){ Parent._lev=true; }
This ensures that when creating an instance of this object, the attribute method will not be created repeatedly.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.