Before, I was a little confused about prototype inheritance and identifier search in Javascript's prototype chain.
For example, the following code:
The code copy is as follows:
function Foo() {};
var foo = new Foo();
Foo.prototype.label = "laruence";
alert(foo.label); //output: laruence
alert(Foo.label);//output: undefined
Today I saw the following picture:
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 function in the process of searching the prototype chain.
Today I found under firefox (because firefox exposed [[prototype]] through __proto__), the one who really participates in the identifier search is the __proto__ of the function object,
The code copy 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:
The code copy is as follows:
function Foo() {};
alert(Foo.__proto__ === Foo.prototype); //output: false
In addition, it also explained,
The code copy 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