欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Javascript原型鏈和原型的一個(gè)誤區(qū)

 更新時(shí)間:2014年10月22日 15:57:44   投稿:hebedich  
這篇文章主要介紹了Javascript原型鏈和原型的一個(gè)誤區(qū),需要的朋友可以參考下

之前我對(duì)Javascript的原型鏈中, 原型繼承與標(biāo)識(shí)符查找有些迷惑,

如, 如下的代碼:

復(fù)制代碼 代碼如下:

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

今天看到了如下這個(gè)圖:

Javascript object layout
另外, 在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.

也就是說, 函數(shù)對(duì)象的prototype并不作用于原型鏈查找過程中,

今天在firefox下發(fā)現(xiàn)(因?yàn)閒irefox通過__proto__暴露了[[prototype]]), 真正參與標(biāo)識(shí)符查找的是函數(shù)對(duì)象的__proto__,

復(fù)制代碼 代碼如下:

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

而, 顯然的:

復(fù)制代碼 代碼如下:

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

另外, 也解釋了,

復(fù)制代碼 代碼如下:

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

相關(guān)文章

最新評(píng)論