Firefox和chrome在constructor.prototype之间的行为区别?

时间:2012-04-04 19:46:12

标签: javascript firefox dom google-chrome

经过这么多时间的试验,我发现__proto__Object.getPrototypeOf()方法是遍历DOM对象中原型链的正确方法。

使用一系列constructor.prototype实际上并不遍历两个浏览器中的原型链。(虽然这是ECMA标准中定义的方式,但构造函数的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

2 个答案:

答案 0 :(得分: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);

原型和 proto 完全不同......

如果你有这样的构造函数:

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

然后你可以通过原型向这个构造函数添加方法(我假设你有一个id为“myInstance”的空div和另一个在文档中带有id“test”的文件):

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;   
}

然后,您可以使用new运算符实例化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

我不知道这是否与您谈了一点,但我希望这会帮助您进行搜索。 最好的祝福。 尼古拉斯

答案 1 :(得分:0)

我认为你误解了构造函数/原型是如何工作的。

给定一个构造函数,它的.prototype将是用它构造的东西的原型。原型的.constructor指向构造函数。

特别是,Element.prototype.constructor === Element应该成立。由于错误,它不一定在浏览器中。这就是为什么你在Chrome中看到p2 == p3的原因。