The prototype chain in JavaScript prototype introduction _javascript Tips

Source: Internet
Author: User

Inheritance in JavaScript is accomplished through a prototype chain (prototype chain): There is another object inside each object that exists as its prototype, and the object inherits the property from the prototype. For each object, you can access its prototype object in the following three ways:

1.__proto__. The object's __proto__ property can be used to access its prototype object. This property is supported only in Firefox, Safari, and Chrome, and is not supported in IE and opera.

2.object.getprototypeof (). You can pass an object as a parameter to the Object.getprototypeof () method, which returns the object's prototype object after execution. This method is supported only in the ECMAScript 5 standard.

3.o.constructor.prototype. The prototype object is accessed by first obtaining the constructor function of the object, and then by accessing the prototype property of the constructor function. The premise of using this method is that there is a constructor property in the object that points to the constructor.

The Isprototype () method can be used to determine whether a prototype chain relationship exists between two objects:


Copy Code code as follows:

var p = {X:1};
var o = object.create (p);
Console.log (p.isprototypeof (o));//true


For all objects created in literal quantities, their prototype objects are Object.prototype (as a special object, Object.prototype has no prototype object):


Copy Code code as follows:

var x = {a:18, b:28};
Console.log (x.__proto__);//object {}


For all objects created with the new operator, the prototype object is the prototype property of the constructor function:


Copy Code code as follows:

var x = {a:18, b:28};
function Test (c) {
THIS.C = C;
}
Test.prototype = x;
var t = new Test (38);
Console.log (t);//object {c=38, a=18, b=28}
Console.log (t.__proto__);//object {a=18, b=28}
Console.log (t.__proto__.__proto__);//object {}


The process of creating objects using the new operator in JavaScript is as follows:

1. Create a new empty object.
2. Point the __proto__ property of this object to the prototype property of the constructor function.
3. Use this object as the this parameter to perform the constructor function.

From the above creation process, it can be concluded that all objects constructed from the same constructor function have the same __proto__ properties (i.e. their prototype objects), that is, only one prototype object exists:


Copy Code code as follows:

var t = new Test (38);
var t2 = new Test (68);
Console.log (t = = = t2);//false
Console.log (t.__proto__ = = = t2.__proto__);//true

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.