Home > Web Front-end > JS Tutorial > body text

A misunderstanding about Javascript prototype chain and prototype_javascript skills

WBOY
Release: 2016-05-16 16:33:04
Original
1434 people have browsed it

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
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template