Creating an `object` tag with `createElement` returns a function instead of an object

后端 未结 1 1948
难免孤独
难免孤独 2021-01-06 23:00

Could someone explain why I am getting different types when creating different elements?

typeof document.createElement(\'div\')
\"object\"
typeof document.cr         


        
1条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-06 23:38

    The ES5 spec states that an object that implements [[Call]] must be reported as typeof "function".

    A small handful of DOM elements (object, embed, not many others) are callable / do implement [[Call]]. This is likely for historical reasons, but the fact remains that you can call them.

    So Firefox is technically correct in reporting them as functions. You could, in fact, argue that Chrome et al. are wrong (if sticking strictly to the ES5 spec) to report them as objects, since they are callable in those browsers too. On the other hand, those browsers are doing what most people would probably 'expect'.

    More details are in Bugzilla.

    0 讨论(0)
提交回复
热议问题