Home >Web Front-end >JS Tutorial >Introduction to prototype chain prototype in JavaScript_javascript skills

Introduction to prototype chain prototype in JavaScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:23:041477browse

Inheritance in JavaScript is accomplished through the prototype chain: each object has another object inside it as its prototype, and the object inherits properties from this prototype. For each object, you can access its prototype object in the following three ways:

1.__proto__. An object's prototype object can be accessed through its __proto__ attribute. This property is only supported in Firefox, Safari and Chrome, not IE and Opera.

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

3.o.constructor.prototype. Access the prototype object by first obtaining the constructor function of the object, and then accessing the prototype property of the constructor function. The prerequisite for using this method is that there is a constructor attribute pointing to the constructor in the object.

To determine whether there is a prototype chain relationship between two objects, you can use the isPrototype() method:


Copy code The code is as follows:

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


For all objects created with literals, their prototype objects are Object.prototype (as a special object, Object.prototype has no prototype object):


Copy code The code is as follows:

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


For all objects created with the new operator, their prototype objects are the prototype attributes of the constructor function:


Copy code 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 objects 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 this parameter to execute the constructor function.

From the above creation process, it can be concluded that all objects constructed from the same constructor function have their __proto__ attributes (that is, their prototype objects) equal, that is, there is only one prototype object:


Copy code 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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn