Why is document.all falsy?

前端 未结 4 1437
难免孤独
难免孤独 2020-11-29 21:52

document.all is a non-primitive object in the DOM that is falsy.

For example, this code doesn\'t do anything:

if (document.all) {
    al         


        
4条回答
  •  一个人的身影
    2020-11-29 22:09

    ES2019 Update

    There is now an [[IsHTMLDDA]] internal slot for objects:

    An [[IsHTMLDDA]] internal slot may exist on implementation-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.

    The HTML Standard has also been updated to add that internal slot for objects that implement the HTMLAllCollection interface:

    Objects that implement the HTMLAllCollection interface are legacy platform objects with an additonal [[Call]] internal method described in the section below. They also have an [[IsHTMLDDA]] internal slot.


    The reason for this madness is specified in this note in the HTML Standard:

    These special behaviors are motivated by a desire for compatibility with two classes of legacy content: one that uses the presence of document.all as a way to detect legacy user agents, and one that only supports those legacy user agents and uses the document.all object without testing for its presence first.

    So basically the standard wants to be compatible with these two types of code:

    • Code that checks if it is running inside Internet Explorer to use its non-standard features, like document.all and Activex;

      if (document.all) {
          useActiveXStuff();
      }
      
    • Code that assumes it's running inside Internet Explorer and uses document.all.

      document.all["my-button"].onclick = function () {
          alert("hi");
      };
      

提交回复
热议问题