admin管理员组文章数量:1297042
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?
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?
Share Improve this question edited May 1, 2012 at 7:01 Mathias Bynens 150k54 gold badges222 silver badges250 bronze badges asked Apr 27, 2012 at 10:28 Nicola PeluchettiNicola Peluchetti 76.9k32 gold badges149 silver badges195 bronze badges4 Answers
Reset to default 9Disclaimer: 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 patibility 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.)
It is document.all
.
It's non-standard, so you're better off using document.getElementsByTagName("*")
.
Ok, using this code
for (var name in document) {
if (!!document[name] === false && typeof document[name] === 'object' && document.hasOwnProperty(name)) {
$('#foo').append('document.' + name + '<br />');
};
};
i had this result in chrome (results may vary)
document.ownerDocument
document.attributes
document.namespaceURI
document.nextSibling
document.webkitCurrentFullScreenElement
document.nodeValue
document.preferredStylesheetSet
document.textContent
document.previousSibling
document.parentNode
document.xmlVersion
document.parentElement
document.localName
document.selectedStylesheetSet
document.prefix
document.xmlEncoding
Just loop over the document and test all..
http://jsfiddle/UTNkW/3/
EDIT: Wrong test methodology, thankfully someone pointed it out and I could correct it.
本文标签:
版权声明:本文标题:All objects in JavaScript are truthy per the spec, but in the DOM one non-primitive object is not. Which? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741647641a2390282.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论