gpt4 book ai didi

javascript - IE9 : hasOwnProperty trick does not work?

转载 作者:行者123 更新时间:2023-11-29 10:20:22 27 4
gpt4 key购买 nike

有据可查的是,IE 中的原生 DOM 元素不包含 hasOwnProperty() 方法。有几个解决方案;其中最优雅的是直接在 Object.prototype 中访问 hasOwnProperty() 方法,如下所示:

Object.prototype.hasOwnProperty.call(element, name);

在我看来,这在 IE9 中不再有效。有人可以解释吗?这是一个 fiddle说明这一点。

在关闭兼容模式的标准模式下使用 IE9,警告显示:

sessionStorage is supported: false
localStorage is supported: false

但是,在打开兼容性或 Quirks 模式(或使用开发人员工具使用 IE8 和 IE7 引擎呈现页面)后,警告显示:

sessionStorage is supported: true
localStorage is supported: true

这里是否还有其他问题,或者 hasOwnProperty 的解决方法是否不再可用?

最佳答案

因为那些属性是保存在window的原型(prototype)链中,而不是直接保存在对象上。

如果您使用in,它会为您搜索原型(prototype)链,并在IE9 中为您提供true

var isSessionStorageSupported = "sessionStorage" in window; // true
var isLocalStorageSupported = "localStorage" in window; // true

http://jsfiddle.net/fHRZs/2/

所以 .hasOwnProperty() 给出了正确的结果。

关于javascript - IE9 : hasOwnProperty trick does not work?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13480745/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com