fakedrake
fakedrake

Reputation: 6856

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

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

typeof document.createElement('div')
"object"
typeof document.createElement('span')
"object"
typeof document.createElement('embed')
"function"
typeof document.createElement('object')
"function"

These are on firefox:

$ firefox --version

(process:9436): GLib-CRITICAL **: g_slice_set_config: assertion 'sys_page_size == 0' failed
Mozilla Firefox 32.0.3

Upvotes: 2

Views: 369

Answers (1)

Shai
Shai

Reputation: 7317

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.

Upvotes: 3

Related Questions