Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

js中如何创建一个undifined的对象(函数),内含有值。例如 typeof(a) = undifined

原因是发现document.all方法的Boolean值为false,
image.png

而使用typeof(document.all) 返回值是undifined,
image.png

但是document.all确实是有值的。
image.png

是否有某些方法可以重写对象本身的Boolean值的方法呢?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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.
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.

ES 规范定义了一种 [[IsHTMLDDA]] internal slot 的内部属性,拥有这个属性的对象,特点就是你看到的,typeof 值是 undefined,Boolean 值是 false。
而这个属性现在仅被 document.all 使用,document.all 是早年 IE 定义的浏览器方法,其它浏览器也沿用下来了,但现在已经被 Web 规范废弃了,现代规范为了向下兼容,才给它定义了这么奇怪的特性,可以看成是一种例外。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...