CopyCodeThe Code is as follows: function flower ()
{
This. Name = "Rose ";
This. color = "red ";
}
// Flower () as the constructor
VaR OBJ = new flower ();
// Output True, flower as class reference
Alert (OBJ instanceof flower );
The function keyword can declare a common function, which is the same as the function concept in other languages. In addition, it can be used for class declaration and implementation, object constructor, and class reference.
In the preceding example, the flower class is declared using the function keyword, and two attributes, name and color, are declared using the this keyword. Then, when you create an OBJ object, flower plays the role of object constructor again. Finally, the instanceof keyword is used to determine whether the OBJ object is an instance of the flower class. At this time, flower plays the role of class reference.