JavaScript中的所有对象都符合规范,但在DOM中,一个非原始对象不是。哪一个? [英] All objects in JavaScript are truthy per the spec, but in the DOM one non-primitive object is not. Which?
问题描述
说:
全部
属性必须返回一个HTMLAllCollection
根据文档
节点,其过滤器匹配所有元素。
返回的对象所有这些都有几个不寻常的行为:
用户代理必须像JavaScript $中的
ToBoolean()
运算符一样b $ b将全部
返回的对象转换为false
值。
为了
==
和!=
$的目的,用户代理必须采取行动b $ b操作符在JavaScript中,返回的对象全部
等于
undefined
value
用户代理必须使JavaScript
中的typeof
运算符返回字符串'未定义'
应用于
所返回的对象
。
这些要求是故意违反JavaScript
规范当前的写作(ECMAScript第5版)。
JavaScript规范要求将ToBoolean()
operator
将所有对象转换为true
价值,并且没有为
的条款
作为$ code>未定义为某些运营商的
的目的。这种违规行为是由于与两类遗留内容的b $ b兼容性的愿望,一种使用
存在的document.all
作为一种方式检测遗留用户代理和
一个只支持那些旧用户代理,并使用
document.all
对象,而不首先测试它的存在。 / p>
所以,$ code> document.all 是这个ECMAScript规则的唯一官方例外。 (在Opera中, document.attachEvent
等也是虚假的,但这并不是任何地方。)
There is this tweet on Twitter:
In JavaScript, all objects are truthy (as per the spec). In the DOM, there’s one exception to this rule. What is it? #jsquiz #fronttrends
Does anyone know the answer?
Disclaimer: I’m the guy who tweeted that :) It was a question I would ask and answer in my Front-Trends talk. I wrote that tweet 5 minutes before going on stage.
Because of the 140-character limit on Twitter, the question is slightly ambiguous. The real question I was asking is the following.
The ECMAScript spec defines ToBoolean()
as follows:
As you can see, all non-primitive objects (i.e. all objects that aren’t a boolean, a number, a string, undefined
, or null
) are truthy as per the spec. However, in the DOM, there is one exception to this — a DOM object that is falsy. Do you know which one that is?
The answer is document.all
. The HTML spec says:
The
all
attribute must return anHTMLAllCollection
rooted at theDocument
node, whose filter matches all elements.The object returned for all has several unusual behaviors:
The user agent must act as if the
ToBoolean()
operator in JavaScript converts the object returned forall
to thefalse
value.The user agent must act as if, for the purposes of the
==
and!=
operators in JavaScript, the object returned forall
is equal to theundefined
value.The user agent must act such that the
typeof
operator in JavaScript returns the string'undefined'
when applied to the object returned forall
.These requirements are a willful violation of the JavaScript specification current at the time of writing (ECMAScript edition 5). The JavaScript specification requires that the
ToBoolean()
operator convert all objects to thetrue
value, and does not have provisions for objects acting as if they wereundefined
for the purposes of certain operators. This violation is motivated by a desire for compatibility with two classes of legacy content: one that uses the presence ofdocument.all
as a way to detect legacy user agents, and one that only supports those legacy user agents and uses thedocument.all
object without testing for its presence first.
So, document.all
is the only official exception to this ECMAScript rule. (In Opera, document.attachEvent
etc. are falsy too, but that’s not specced anywhere.)
这篇关于JavaScript中的所有对象都符合规范,但在DOM中,一个非原始对象不是。哪一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!