2012-04-04 75 views
6

經過這麼多時間的實驗,我發現__proto__Object.getPrototypeOf()方法是在DOM對象中遍歷原型鏈的正確方法。 (雖然這是ECMA標準中定義的方式,但構造函數的prototype屬性是您的原型對象)。使用一系列constructor.prototype實際上並不遍歷兩個瀏覽器中的原型鏈。Firefox和chrome行爲在constructor.prototype之間的區別?

任何建議或意見被歡迎...

p1 = document.getElementById("test"); // div element 

//Prototype Object of p1 
p2 = element.constructor.prototype; 

//Prototype object of p2 
p3 = element.constructor.prototype.constructor.prototype; 

console.log(p2 === p3); // true in chrome(howcome they same ?), false in firefox 

q2 = element.__proto__; 
q3 = element.__proto__.__proto__; 

console.log(q2 === q3); // false in both browser 

回答

0

我想你誤會了構造函數/原型是如何工作的。

給定一個構造函數,它的.prototype將成爲用它構造的東西的原型。原型的.constructor指向構造函數。

所以特別是,Element.prototype.constructor === Element應該保持。由於錯誤,它不一定在瀏覽器中。這就是爲什麼你在Chrome中看到p2 == p3的原因。

1

我完全鮑里斯...... 同意你應該在這裏尋找更多細節(https://www.google.com/search?q=javascript+prototype+chain),但基本上,如果你想要瀏覽的DOM對象的元素,你只需要做到這一點像下文:

function exploreElement(element){ 
     contentToString = ""; 
     for (var i in element){ 
      contentToString += i + " : " + element[i] + "<br />"; 
     } 
     document.write(contentToString); 
    } 
    exploreElement(document); 

原型和是完全不同的東西...

如果你有一個構造函數是這樣的:

function SomeObject(){ 
     this.__proto__.id = "instance_default_name"; 
     SomeObject.id = "SomeObject"; 
     // __proto__ HERE to access the prototype!!! 
    } 

然後,您可以添加方法來通過原型這個構造函數(我假設你有一個ID「將myInstance」,另一個文檔中的ID爲「測試」的空格):

SomeObject.prototype.log = function(something){ 
     document.getElementById("myInstance").innerHTML += something + "<br />"; 
    } 

添加一些用於測試目的方法:

SomeObject.prototype.setId = function(id){ 
    this.id = id; 
} 
SomeObject.prototype.getId = function(){ 
    return this.id; 
} 
SomeObject.prototype.getClassName = function(){ 
    return SomeObject.id; 
} 

然後,您可以實例SomeObject與新的運營商,並做一些測試是這樣的:

myInstance = new SomeObject(); 
myInstance.setId("instance_1"); 
aDivElement = document.getElementById("test"); 
aDivElement.style.backgroundColor = "rgb(180,150,120)"; 
myInstance.log("p1 = " + aDivElement); 
// [object HTMLDivElement] 
myInstance.log("p1 backgroundColor = " + (aDivElement.style.backgroundColor)); 
myInstance.log("myInstance = " + myInstance); 
// [object Object] an instance of SomeObject 
myInstance.log("myInstance.constructor = " + myInstance.constructor); 
// function SomeObject() { this.__proto__.id = "instance_default_name"; SomeObject.id = "SomeObject"; } 
myInstance.log("myInstance.constructor.prototype = " + myInstance.constructor.prototype); 
// .prototype WHEN CALLED by the instance NOT __proto__ 
// The constructor of myInstance is SomeObject and the prototype of SomeObject is the prototype of all instances of SomeObject 
myInstance.log("myInstance.id = " + myInstance.getId()); 
// id for the instance of SomeObject that you have instanciated 
myInstance.log("SomeObject.prototype.id = " + SomeObject.prototype.getId()); 
// id by default of the prototype 
myInstance.log("myInstance.constructor.prototype.id = " + myInstance.constructor.prototype.getId()); 
// id by default of the prototype 
myInstance.log("myInstance.getClassName() = " + myInstance.getClassName()); 
// myInstance.getClassName() = SomeObject 

我不知道這是否會與你談一點,但我希望這會幫助你進行搜索。 此致敬禮。 Nicolas