JavaScript Prototype Learning Summary _javascript skills

Source: Internet
Author: User
Tags function prototype
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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.