Javascript 为什么这个.hasOwnProperty(';window';)在chrome中是真的,而在IE11中是假的?
我运行以下代码:Javascript 为什么这个.hasOwnProperty(';window';)在chrome中是真的,而在IE11中是假的?,javascript,internet-explorer-11,Javascript,Internet Explorer 11,我运行以下代码: (function () { console.log(this,this.hasOwnProperty('window')) })(); 在chrome中,它输出 [窗口]正确 在IE11中,it输出 [窗口]错误 为什么?IE完全弄乱了hasOwnProperty方法,因为主机对象很痛苦(主机对象没有hasOwnProperty方法) 但是,我们可以直接访问对象.prototype,以确保任何hasOwnProperty调用都没有被篡改或重写 var myObjec
(function () {
console.log(this,this.hasOwnProperty('window'))
})();
在chrome中,它输出
[窗口]正确
在IE11中,it输出
[窗口]错误
为什么?IE完全弄乱了hasOwnProperty方法,因为主机对象很痛苦(主机对象没有hasOwnProperty方法) 但是,我们可以直接访问
对象.prototype
,以确保任何hasOwnProperty调用都没有被篡改或重写
var myObject= {
prop: 'MyName',
otherProp: null
};
if (Object.prototype.hasOwnProperty.call(toddObject, 'favouriteDrink')) { // true
// do something if it exists
}
这里的秘密是.call()
更改hasOwnProperty的上下文(即,获取该上下文),并确保我们从Object.prototype中获得所需的确切hasOwnProperty。的可能副本