Prototype in JavaScript _ javascript skills

Source: Internet
Author: User
This article mainly introduces prototype in JavaScript. This article describes three methods for accessing prototype objects and how to determine whether there is a prototype link between two objects, if you need a prototype chain, you can refer to the inheritance in JavaScript through prototype chain: each object has another object as its prototype, the object inherits the property from this prototype ). For each object, you can use the following three methods to access its prototype object:

1. _ proto __. You can access the prototype object through the _ proto _ attribute of the object. This attribute is only supported in Firefox, Safari, and Chrome, but not in IE and Opera.

2. Object. getPrototypeOf (). You can pass an Object as a parameter to the Object. getPrototypeOf () method. after execution, the prototype Object of the Object is returned. This method is only supported in the ECMAScript 5 standard.

3. o. constructor. prototype. Obtain the constructor function of the object first, and then access the prototype attribute of the constructor function to access the prototype object. The premise of using this method is that the object has the constructor attribute pointing to the constructor.

To determine whether there is a prototype link between two objects, use the isPrototype () method:


The code is as follows:


Var p = {x: 1 };
Var o = Object. create (p );
Console. log (p. isPrototypeOf (o); // true


For all objects created literally, the prototype Object is Object. prototype (as a special Object, Object. prototype does not have a prototype Object ):


The code is as follows:


Var x = {a: 18, B: 28 };
Console. log (x. _ proto _); // Object {}


For all objects created using the new operator, the prototype object is the prototype attribute of the constructor function:


The code is 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 using the new operator to create an object in JavaScript is as follows:

1. create a new empty object.
2. point the _ proto _ attribute of this object to the prototype attribute of the constructor function.
3. use this object as the this parameter and execute the constructor function.

From the above creation process, we can draw a conclusion: all the objects constructed from the same constructor function have the _ proto _ attribute (that is, its prototype object) equal, that is, only one prototype object exists:


The code is as follows:


Var t = new Test (38 );
Var t2 = new Test (68 );
Console. log (t = t2); // false
Console. log (t. _ proto _ = t2. _ proto _); // true

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.