原因是发现document.all方法的Boolean值为false,
而使用typeof(document.all) 返回值是undifined,
但是document.all确实是有值的。
是否有某些方法可以重写对象本身的Boolean值的方法呢?
An [[IsHTMLDDA]] internal slot may exist on host-defined objects. Objects with an [[IsHTMLDDA]] internal slot behave like undefined in the ToBoolean and Abstract Equality Comparison abstract operations and when used as an operand for the typeof operator.
typeof
Objects with an [[IsHTMLDDA]] internal slot are never created by this specification. However, the document.all object in web browsers is a host-defined exotic object with this slot that exists for web compatibility purposes. There are no other known examples of this type of object and implementations should not create any with the exception of document.all.
document.all
ES 规范定义了一种 [[IsHTMLDDA]] internal slot 的内部属性,拥有这个属性的对象,特点就是你看到的,typeof 值是 undefined,Boolean 值是 false。而这个属性现在仅被 document.all 使用,document.all 是早年 IE 定义的浏览器方法,其它浏览器也沿用下来了,但现在已经被 Web 规范废弃了,现代规范为了向下兼容,才给它定义了这么奇怪的特性,可以看成是一种例外。
[[IsHTMLDDA]] internal slot
2.1m questions
2.1m answers
60 comments
57.0k users