Excerpted from JavaScript advanced Program Design.
Javascript is an object-oriented language and naturally has some object-oriented features.
I. constructor Method
Step 1 select the class name, that is, the name of the constructor. The following example does not create an object in the constructor, but uses this to execute the first line when calling the constructor using the new operator. Code First, create an object. This object can only be accessed with this, and then you can directly assign this attribute. By default, it is the return value of the constructor without returning.
Example:
Function Car (scolor, idoors)
{
This . Color = Scolor;
This . Doors = Idoors;
This . Showcolor = Function ()
{
Alert (This. Color );
} ;
}
VaR Ocar1 = New Car ( " Red " , 4 ); // Output red
VaR Ocar2 = New Car ( " Blue " , 3 ); // Output blue
The preceding example creates an independent function version for each object. Let's look at the following prototype method.
Ii. prototype:
This method uses the prototype attribute of an object to view it as the prototype on which the object is created. The following example uses an empty constructor to set the class name, and then all attributes and methods are directly assigned to the prototype attribute. When new car () is called, all attributes of the prototype are immediately assigned to the object to be created.
Example: Function Car ()
{
}
Car. Prototype. Color = " Red " ;
Car. Prototype. Doors = 4 ;
Car. Prototype. showcolor = Function ()
{
Alert (This. Color );
} ;
VaR Ocar1 = New Car (); // Output red
In the preceding example, after multiple instances are created, the object property value has not changed. Let's look at the following mixed Constructors/prototype.
Iii. Mixed Constructors/prototype:
Using constructor and prototype together, you can create objects like Yang, another programming language. Use constructor to define all non-function attributes of an object and define the function attributes of an object in prototype. Function Car (scolor, idoors)
{
This. Color=Scolor;
This. Doors=Idoors;
}
Car. Prototype. showcolor = Function ()
{
Alert (This. Color );
} ;
VaR Ocar1 = New Car ( " Red " , 4 );
VaR Ocar2 = New Car ( " Blue " , 3 );
Ocar1.showcolor (); // Output red
Ocar2.showcolor (); // Output blue
4. Dynamic Prototype Method:
The basic idea of the dynamic prototype method is the same as that of the mixed constructor/prototype method. The only difference is that the object method is assigned a location. See the following example: Function Car (scolor, idoors)
{
This . Color = Scolor;
This . Doors = Idoors;
If ( Typeof Car. initialized = " Undefined " ) // ***
{
Car. Prototype. showcolor = Function ()
{
Alert (This. Color );
} ;
Car. initialized = True ; // ***
}
}
The above method uses the flag initialized to determine whether any method has been assigned to the prototype. This method is created and assigned only once.
V. Hybrid factory Mode
The purpose of this method is to create a false constructor and return only new instances of one object: Function Car ()
{
VaR Tempcar = New Object;
Tempcar. Color = " Red " ;
Tempcar. Doors = 4 ;
Tempcar. showcolor = Function ()
{
Alert (This. Color );
} ;
Return Tempcar;
}
VaR Ocar1 = New Car ();
Because the new operator is called inside the car () constructor, 2nd new operators outside the constructor are ignored. This method is not recommended.
You can use the prototype attribute to define new methods for any existing classes.