Home  >  Article  >  Web Front-end  >  A misunderstanding about Javascript prototype chain and prototype_javascript skills

A misunderstanding about Javascript prototype chain and prototype_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:33:041399browse

Before, I was a little confused about prototypal inheritance and identifier lookup in Javascript’s prototype chain,

For example, the following code:

Copy code The code is as follows:

function Foo() {};
var foo = new Foo();
Foo.prototype.label = "laruence";
alert(foo.label); //output: laruence
alert(Foo.label);//output: undefined

I saw the following picture today:

Javascript object layout
Also, see in Javascript Object Hierarchy:

The prototype is only used for properties inherited by objects/instances created by that function. The function itself does not use the associated prototype.

In other words, the prototype of the function object does not affect the prototype chain search process,

I discovered today under firefox (because firefox exposed [[prototype]] through __proto__), what actually participates in identifier search is the __proto__ of the function object,

Copy code The code is as follows:

function Foo() {};
var foo = new Foo();
Foo.__proto__.label = "laruence";
alert(Foo.label); //output: laruence
alert(foo.label);//output: undefined

And, obviously:

Copy code The code is as follows:

function Foo() {};
alert(Foo.__proto__ === Foo.prototype); //output: false

In addition, it is also explained,

Copy code The code is as follows:

alert(Object.forEach); // undefined

Function.prototype.forEach = function(object, block, context) {
for (var key in object) {
If (typeof this.prototype[key] == "undefined") {
               block.call(context, object[key], key, object);
}
}

};

alert(Object.forEach);
alert(Function.forEach);
alert(Object.forEach === Function.forEach); // 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