I understand why you need to use Object.prototype.toString()
or String()
for typechecking arrays, but isn't typeof sufficient for typechecking functions and strings? For example the polyfill on MDN for Array.isArray uses:
Object.prototype.toString.call(arg) == '[object Array]';
It's pretty clear in the case of arrays because you can't use typeof
to check for arrays. Valentine uses instanceof for this:
ar instanceof Array
But for strings/functions/booleans/numbers, why not use typeof
?
jQuery and Underscore both use something like this to check for functions:
Object.prototype.toString.call(obj) == '[object Function]';
Isn't that equivalent to doing this?
typeof obj === 'function'
or even this?
obj instanceof Function