All objects in JavaScript are truthy per the spec, but in the DOM one non-primitive object is not. Which?

后端 未结 4 2096
日久生厌
日久生厌 2020-12-16 18:38

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? #

4条回答
  •  执念已碎
    2020-12-16 19:27

    Ok, using this code

    for (var name in document) {
        if (!!document[name] === false && typeof document[name] === 'object' && document.hasOwnProperty(name)) {
            $('#foo').append('document.' + name + '
    '); }; };​

    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
    

提交回复
热议问题