经过这么长时间的试验,我发现__proto__或Object.getPrototypeOf()方法是遍历DOM对象中原型链的正确方法。
使用一系列原型实际上不会遍历两个浏览器中的原型链(虽然这是ECMA标准中定义的方式,但构造器的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发布于 2012-04-06 00:01:32
我完全同意鲍里斯的观点。你应该在这里搜索更多细节(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!!!
}然后,您可以通过prototype向此构造函数添加方法(我假设您在文档中有一个id为"myInstance“的空div和一个id为"test”的空div):
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我不知道这对你是否有一点帮助,但我希望这会对你的搜索有所帮助。诚挚的问候。尼古拉斯
发布于 2012-04-05 12:14:57
我想你误解了构造器/原型的工作原理。
给定一个构造函数,它的.prototype将是用它构造的东西的原型。原型的.constructor指向构造器函数。
因此,尤其是,Element.prototype.constructor === Element应该保持。由于错误,浏览器中不一定会出现这种情况。这就是为什么你在Chrome中看到p2 == p3的原因。
https://stackoverflow.com/questions/10018198
复制相似问题