I\'m writing a JavaScript Library that offers the function tablify(anything);
, which is able to represent any Array or Object as an HTML Table.
Now I\'
Why is
typeof
always returning"object"
?
Because in sloppy mode, the this context always is supposed to be an object. When you call a function or pass undefined or null, you get the global object, when you call a method the context will get casted to an object.
Use strict mode for your tablify
method and it will work!
Is it a good thing for a JS-API to provide this sort of functionality (extending Arrays/Objects with
.tablify();
)?
Yes. No.. Many people will frown to use your script when you want to share it. See Why is extending native objects a bad practice? for a detailed discussion.
At least you've properly used non-enumerable properties - but I would recommend to use them on Array.prototype
as well, too many people abuse for in
enumerations.
How is it possible to distinguish between "normal" objects, numbers, functions,... ?
typeof
seems fine.
Is it possible to only extend "normal" objects? (forbid (42).tablify();, "string".tablify(); ...)
Not really. All primitive wrappers inherit from Object.prototype
.
Whats the name for those "normal" objects? How should I call them?
Just "objects". Or "plain objects" if you want (distinguishes them from arrays, built-ins, host objects).