A deep understanding of the JavaScript prototype chain and a deep understanding of javascript
Preface
I have encountered a question recently. You can try it.
Object.prototype.a = function() { console.log("aaa "+this.name);};Function.prototype.b = function() { console.log("bbb "+this.name);};function Person(name) { this.name = name;}var person = new Person("China");
Q: What are returned by person. a () person. B?
Document reference
The above questions are about the JavaScript prototype chain. We reference the prototype Object Description in JavaScript advanced programming (Third edition.
-List content
Whenever a new function is created, a prototype attribute is created for the function according to a set of specific rules. This attribute points to the prototype object of the function.
After a constructor is called to create a new instance, the instance contains a pointer (internal attribute) pointing to the constructor's prototype object. In ECMA-262 5th, this pointer is called [Prototype]. There is no standard way to access [[Prototype] in the script, but the browser supports a property _ proto _ on each object __. The connection exists between the instance and the prototype object of the constructor, rather than between the instance and the constructor.
Analyze problems
Based on the above description, let's take a look at the question. The constructor Person has a prototype attribute pointing to the prototype object of the Person. While Person. prototype is also an Object, there is a [[Prototype] pointer inside, pointing to the prototype Object of the constructor, And the constructor of this Object is Object ().Person.prototype.__proto__ -----> Object.prototype
. The internal pointer [[Prototype] of the Instance person points to the prototype Person. Prototype of the constructor Person.
Solve the problem
Through the above analysis, we have constructed a prototype chain:
person.\__ptoto__ -----> Person.prototype
So person. a () searches through the prototype chain and finds method a in Object. prototype. The console outputsAaa ChinaBut Method B is not found. Therefore, an error is reported.TypeError: undefined is not a function
In-depth understanding
In this case, the constructor Person is also an instance of the Function, so Person. _ proto _ --> Function. prototype. The constructor inherits Prerson. prototype rather than Person. \__ proto __.
You can use isPrototypeOf () to determine whether an object is a prototype of another object.
Person.prototype.isPrototypeOf(person); //true
In addition, ECMA5 adds a method Object. getPrototypeOf () to replace _ proto __, and returns the value of [[Prototype.
For the instanceof operator, to determine whether an object belongs to a constructor is to determine whether the prototype attribute of the constructor exists in the prototype chain of the object.
Person instanceof Function; //true
Although the returned result of Object. getPrototypeOf (Function) is function Empty () {},
If Function instanceof Object still returns true, it can only be regarded as the hip of the turtle.
In addition, ECMA5 provides an inheritance method Object. create (proto, [propertiesObject]) to create an Object with the specified prototype and several specified attributes.
// Only inherit Person. prototype without instantiating the name attribute var person = Object. create (Person. prototype); // However, the following is still trueperson instanceof Person;
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.