IE8 querySelector null vs normal null [英] IE8 querySelector null vs normal null

查看:60
本文介绍了IE8 querySelector null vs normal null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在ie8中发现了非常有趣的行为。事实证明null并不总是null。

I just found really interesting behaviour in ie8. It turns out null is not always null.

// just normal, casual null hanging out in the sun
var nullA = null;
// query for non existing element, should get null, same behaviour also for getElementById
var nullB = document.querySelector('asdfasfdf');

// they are equal
console.log(nullA === nullB);

// false
nullA instanceof Object;

// will throw 'Object expected' error in ie8. Black magic
nullB instanceof Object;

任何人都有解释?

推荐答案

所以,Jan Dvorak绝对是对的。

So, Jan Dvorak is definitely right.

根据这个答案 null 是一个本机对象, querySelector 是一个宿主对象。

According to this answer, null is a native object and querySelector is a host object.

ECMA规范,因此其行为取决于实施,IE8和IE10有不同的JScript实现,这就是为什么,即使在IE8模式中,IE10中的JavaScript引擎也会以不同的方式处理对象(并且更好)。看来这个特定实现中的特定主机对象违反了Sec 4.3.8要求它的 prototype null 对象因为它似乎没有继承其 instanceOf 值。

Host object behavior is not well defined in the ECMA specification, so its behavior is up to the implementation, and IE8 and IE10 have different JScript implementations, which is why, even in "IE8 Mode" the JavaScript engine in IE10 processes the objects differently (and better). It does appear that this particular host object in this particular implementation would be in violation of Sec 4.3.8 requiring it's prototype to be null or Object as it appears not to have inherited its instanceOf value.

它似乎是IE8实现的JScript(!== ECMAScript || JavaScript)中的一个错误,当它们切换到脉轮引擎

It appears to be a bug in IE8 implementation of JScript (!== ECMAScript || JavaScript) that was fixed when they switched to the Chakra engine.

所有这一切,如果你这样做会伤害,不要那样做。只需检查 document.querySelector()=== null

All that being said, if it hurts when you do that, don't do that. Just check to see if document.querySelector() === null.

希望能够对它有所了解。有关详细信息,请参阅链接的答案,他们做了很好的解释。

Hope that sheds some light on it. For more info, see the linked answer, they did a great job explaining.

这篇关于IE8 querySelector null vs normal null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆