type.js - offers an more consistent type checking of native values.
It relays on the [[Class]] of objects instead on typeof
results.
type2.js - variation that returns "object" for wrapped natives. This one is probably more noob-immune, avoiding some possible strange situations.
Consider something like this:
// Defined by third...
var foo = new String("foo");
// ...
function noob( str ) {
if ( type(str) !== "string" ) {
throw new TypeError("string expected");
}
return ( str === "foo" );
}
If the type check returns "string"
, then noob
will return false
because of the strict equality check ===
. To avoid similar situations, "object" is returned for wrapped strings, numbers and booleans.
Does spec saying that any callable would return "function"? Well, implementations are not consistent.
Even if
typeof
was perfectly consistent, it have to deal with host objects that are not consistent at all (no spec). And in that cases, its behavior is certainly not cross-browser.We are not fixing
typeof
, but providing an alternative.