Continue to explore the prototype properties of JS objects, have seen before the creation of a play, at any time there will be a _proto_ property with all, then, this _proto_ is used for what, the interview asked the tall on the prototype chain is what?
Take out the previous examples, as follows:
function person (name) {
THIS.name = name;
this.interduceself= function () {
Alert ("My name is" + this.name);
};
};
Person.prototype.interduceSelfChinses = function () {
Alert ("My name is" + this.name);
};
As can be seen from this example, by giving Person.prototype a new way to set up a function object, the normal object with the person instance (in the example: Person1) inherits the new method. Then I looked at these two things: person1.__proto__ and Person.prototype, as follows:
Both of them are the same thing, at the same time, I experimented with person1.__proto__ = = Person.prototype, which turns out to be true, so I ventured to speculate on a process when the constructor new person () occurred.
Create a Person1 object;
person1.__proto__ = Person.prototype;
Person.call (' xiaoming ');
Similarly, the Person.prototype object has the __proto__ property, which points to the prototype of the Function object (object) that created it;
person.prototype.__proto__ = = = Object.prototype//result also true
Continue, the Object.prototype object also has the __proto__ attribute, but it is more special, NULL;
object.prototype.__proto__; result is null
We put this chain of __proto__ up until the object.prototype.__proto__ null is called the prototype chain, such as:
__proto__ by definition, is a property of the object, this __proto__ itself is an object, __proto__ properties, can be person1 read to, but will not be displayed in the Person1 object (is a seemingly invisible property), of course _ The property read priority in the _proto__ object is lower than the Person1 itself (which is generally treated as a stealth property and can be used, but it does not have to be specified separately)
The prototype is a self-contained property of the function, where prototype has two elements, constructor and __proto__,
1, constructor points to the function itself;
Person.prototype.constructor = = = person//result is true
Function.prototype.constructor = = = Function//result is true
Object.prototype.constructor = = = Object//result is true
2. __proto__ points to the prototype of the object that created it, i.e.
person1.__proto__ = = Person.prototype//result is true
The most important feature of a constructor in creating an object is that it passes its own prototype by value to the __proto__ of the constructed object, and in the above code, when you execute new person (), you specify prototype and __proto__ for several objects at a time. , take a look at the following structure diagram:
The following summary can be made:
1. Prototype and prototype chain is a model of JS implementation inheritance, on the right, so many seemingly irrelevant objects, through the prototype, prototype chain, unexpectedly to even become in-laws;
2. The formation of the prototype chain is really rely on __proto__ rather than prototype, this is undoubtedly, from the above can be seen, the whole chain relationship, prototype role is the connection point, and the real rope connection is __proto__.
See an example:
function code () {};
function JavaScript () {};
Code.languagetype = "interpreted language";
Javascript.prototype = code;
var jquery = new JavaScript ();
Console.log (Javascript.languagetype)//result is undefined
Console.log (Jquery.languagetype)//result is interpreted language
It's a bit confused not to know the prototype and the prototype chain, so let's explore:
Although the JavaScript function object specifies that the prototype property is code, its __proto__ built-in property does not specify who it is, and the prototype chain is unrelated with the prototype attribute, but relies on __proto__ to find it, Of course there is nothing to find, therefore, this property value cannot be found here Languagetype;
The jquery function object is not the same, it is specified by default at the beginning of the creation of the __proto__ The built-in property is Javascript.prototype, that is, the Languagetype property of the Code,code object is a value, which is interpreted language, so the property value is read.
JS function prototype attribute learning (ii)