javascript - Firefox 和 chrome constructor.prototype 之间的行为差​​异?

标签 javascript firefox dom google-chrome

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

使用一系列constructor.prototype实际上并没有在两个浏览器中遍历原型(prototype)链。(虽然这是ECMA标准中定义的方式,constructor的prototype属性是你的原型(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

最佳答案

我完全同意鲍里斯... 您应该在此处搜索更多详细信息 (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);

原型(prototype)和原型(prototype)是完全不同的东西......

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

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

然后,您可以使用 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

我不知道这是否对您有所启发,但我希望这对您的搜索有所帮助。 最好的祝福。 尼古拉斯

关于javascript - Firefox 和 chrome constructor.prototype 之间的行为差​​异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10018198/

相关文章:

javascript - 如何在 if 语句返回 false 的情况下中止 jquery 函数

javascript - 如何在滚动 N 个像素后取消滚动事件

internet-explorer - 在 Internet Explorer 中工作但在 Firefox 中不起作用

javascript - 播放/暂停视频 onclick firefox

javascript - 如何向 HTML 5 Canvas 添加 2 个或更多 3D 对象?

javascript - 如何禁用浏览器菜单栏中的刷新按钮

firefox - Firebug 不适用于多进程 Firefox

javascript - 在 DOM 对象上设置属性时如何避免无参数重新分配

javascript - 使用 Javascript 获取脚本文件的内容

javascript - Firebug 构造函数与用户函数