Each pair has an implicit TE attribute that is used to point to its parent's image (which is called a parent or implicit prototype). and inherit from its properties and methods [function pairs have an explicit stereotype reference in addition to the parent prototype reference], and in general, the parent of the image is not accessible. And the explicit prototype of function to image can be accessed through functionname.prototype [in Firefox you can access the parent prototype of the image through the __proto__ attribute of the image]
The prototype property itself is an object-type image, so you can add arbitrary properties and methods to this stereotype attribute to allow instances to inherit them from the image.
For example, if we want to add some custom methods to a string type of string.prototype, we can do this (here to add the trim method in a class VBScript as an example)
Copy Code code as follows:
String.prototype.trim=function () {
Return This.replace (/^\s*|\s*$/g, "")
}
"jiangsk540". Trim ();//return "jiagnsk540"
In addition to providing the above features, the prototype provides a group of similar instances to the mechanism of sharing properties and methods [as well as static or static functions, regardless of how many instances of the instance are created with the constructor, the properties and methods defined on the prototype are defined once from beginning to end, All instances share an image that uses this property or method but is not the same as the concept of C + + or Java static properties or static functions.
Copy Code code as follows:
function Class1 (name) {
THIS.name = name;
}
Class1.prototype.show=function () {
Alert ("Name=" +this.name);
}
var m1 = new Class1 ("jiangsk540");
var m2 = new Class1 ("Mao Lion");
alert (m1.show===m2.show);//Show True
A property or method that is dynamically added to a constructor prototype can be immediately invoked by a previously established pair like
Such as
Copy Code code as follows:
function Class1 (name) {
THIS.name = name;
}
Class1.prototype.show=function () {
Alert ("Name=" +this.name);
}
var m1 = new Class1 ("jiangsk540");
Class1.prototype.say=function () {
Alert ("Hi");
}
M1.say ()//Call Success
/*
Note: Only properties or methods added for the stereotype of the constructor can be called immediately if you have created a reference to the stereotype of the constructor if it is a cross-reference to the constructed function prototype then it cannot be called immediately by the created image
* *
CLASS1.PROTOTYPE={NEWP: "jiangsk540"};
Alert (M1.NEWP)//undefined