Prototype understanding
Prototype (prototype)
In JavaScript, the prototype object is an important mechanism for implementing object-oriented.
Each function is an object ( functions), the function object has a sub-object prototype object, and the class is defined in the form of a function.
Prototype represents the prototype of the function, and also represents a collection of members of a class.
in passingWhen new creates an instance object of a class, the members of the prototype object become members of the instantiated object.
1, the object is referenced by the class, only the function object can be referenced;
2, after the new instantiation, its members are instantiated, the instance object can be called.
At the same time, the function is an object, and the function object can be called without being instantiated if it declares the member directly.
Define members:
<script language= "javascript" type= "Text/javascript" >//defines a class that has only one property prop functionClass1 () { This. prop=1; } //Use the prototype property of a function to define a new member for a classClass1.prototype.showprop=function() {alert ( This. Prop); }
Or: Use this method (Qiushi note)
Class1.prototype = {
Showprop:function () {
Alert (This.prop)
}
}
//Create an instance of Class1 varobj1=NewClass1 ();//invoke the Showprop method defined by the prototype prototype objectObj1.showprop ();</script>
JavaScript is Object-based, and any element can be considered an object. However, the types and objects are different.
In this article, in addition to discussing some of the characteristics of types and objects, it is more important to study how to write good and reusable types.
After all, JavaScript, a popular scripting language, is very meaningful for reuse if it can be well encapsulated and form a large type library.
The prototype in JavaScript