【问题标题】:Why don't some objects have a constructor in IE?为什么有些对象在 IE 中没有构造函数?
【发布时间】:2010-10-04 00:03:32
【问题描述】:

下面的 JavaScript 代码在不同的浏览器中有不同的效果:

document.write(this.location.constructor);
document.write("<br/>");
document.write(this.constructor);
document.write("<br/>");

在 Chrome 中,页面有:

function Location() { [native code] }
function DOMWindow() { [native code] }

在 Firefox 中,页面有:

[object Location]
[object Window]

在IE8中,页面有:

undefined
undefined

Chrome 和 Firefox 之间的区别可能是不同的toString() 实现。我只是不明白为什么这两个对象的构造函数在 IE 中是 undefined。如果它们的构造函数是未定义的,那么它们的原型呢?它们也是未定义吗?

【问题讨论】:

    标签: javascript internet-explorer dom object constructor


    【解决方案1】:

    使用 IE8 调试控制台,我使用您提供的代码作为示例没有问题。

    我的结果...

    [object Location]
    [object Window]
    

    您确定没有其他原因导致这些失败吗?

    【讨论】:

    • 我使用的是 IE 8.0.6001.18372。有趣的是,如果我使用“document.write(this.constructor)”加载一个本地 html,它是未定义的;如果我只是在 URL 输入栏中键入“javascript:this.constructor”,它会显示 [object Window]。
    猜你喜欢
    • 2011-05-18
    • 1970-01-01
    • 2015-10-11
    • 2015-03-18
    • 2011-10-22
    • 2011-10-19
    • 2017-06-04
    • 2010-10-24
    • 2011-05-17
    相关资源
    最近更新 更多