5. Custom classes and objects
5.1 factory Method
Create a factory method in ecmascript and return a specific type of object.Code.
Function Createfruit () {
VaR Tempfruit = New Object;
Tempfruit. Name = " Apple " ;
Tempfruit. Number = 5 ;
Tempfruit. showname = Function () {
Alert (This. Name );
} ;
Return Tempfruit;
}
VaR Fruit1 = Creatfruit ();
VaR Fruit2 = Creatfruit ();
You can add a parameter to createfruit () to input the parameter value. With ecmascript being normalized, this method of object creation is no longer popular, partly because of syntax and partly because of functionality, for example, each object instance has its own showname method, which brings some overhead to memory management.
5.2 Constructor
Select a class name. The first letter is in upper case. The class name is the name of the constructor. Creating a constructor is similar to a factory method. The difference is that you need to use the new keyword to create an object reference. Using constructors to create objects has the same drawbacks as using the factory method.
Function Fruit (name, number) {
This . Name = Name;
This . Number = Number;
This . Showname = Function () {
Alert (This. Name );
} ;
}
VaR Fruit1 = New Fruit ( " Apple " , 5 );
VaR Fruit2 = New Fruit ( " Pear " , 3 );
5.3 use prototype
You can use the prototype attribute to create a new object. First, an empty constructor is required to create a class name, and all attributes and methods are directly allocated to the prototype attribute.
Function Fruit () {
}
Fruit. Prototype. Name = " Apple " ;
Fruit. Prototype. Number = 5 ;
Fruit. Prototype. showname = Function () {
Alert (This. Name );
} ;
VaR Fruit1 = New Fruit ();
VaR Fruit2 = New Fruit ();
However, this also has some shortcomings. First, there are no parameters in the constructor, causing some trouble in Initialization. Second, when an attribute points to an object rather than a method, the object will be shared by all instances, any changes will affect the use of other object references.
5.4 mixed use of factory methods and prototype
This concept is simple: Use the constructor to define all attributes except methods, and use prototype to define object methods. In this way, each method is created only once, and each object can have its own object instance attributes.
Function Fruit (name, number) {
This . Name = Name;
This . Number = Number;
This . Owner = New Array ( " Jerry " , " Terry " );
}
Fruit. Prototype. showname = Function () {
Alert (This. Name );
} ;
VaR Fruit1 = New Fruit ( " Apple " , 5 );
VaR Fruit2 = New Fruit ( " Pear " , 3 );
5.5 Dynamic Prototype
Simply put, this method uses an identifier to determine whether prototype has been directed to a method, so as to ensure that these methods are created and directed only once.
5.6 hybrid factory Method
This method is the same as the classic factory method and constructor method in the object Method Memory Management. It is generally not recommended to use this method, except for some special cases (this is an example in XML in Javascript ).
6. Modify an object
You can use the prototype object to modify the object. In addition to custom objects, the original ecmascript object also has the prototype attribute. You can directly use prototype to create a new method for the object.
Number. Prototype. tohexstring = Function () {
Return This. Tostring (16);
} ;
VaR Inum = 10 ;
Alert (inum. tohexstring ()); // Output
In addition, you can use prototype to easily modify existing methods and point the method name to the new method. Note that, after pointing to a new method, the original method will not be used by any object and will be destroyed by the garbage collector, so that the original method will no longer exist. A safer solution is to create a new reference to save the original method, and then overwrite the original method.
Specifically, you can create an object in ecmascript. After an object reference is created, you can add a new method to the object and use it immediately in the object reference. This is a feature of ecmascript, but it is not recommended to avoid unnecessary troubles, such as reading comprehension and documentation.