I've written this "little" typeof
extension for my JS. What do you think about it?
Its aim is to provide a single reliable typeof
in JavaScript, consistent between native vars and objects. I've tested it both in FF 4+, IE 7+, Chrome 12, Safari 5. Also, if you have a different browser (also the version), can you test it?
typeOf = function(e) {
if (typeof e === "undefined") {
return "undefined";
} else if (typeof e === "object") {
if (e === null) {
// null è un oggetto
return "null";
} else if (e instanceof String) {
return "string";
} else if (e instanceof Number) {
return "number";
} else if (e instanceof Boolean) {
return "boolean";
} else if (e instanceof Date) {
return "date";
} else if (e instanceof RegExp) {
return "regexp";
} else if (e instanceof Error) {
return "error";
} else if (e.isArray
|| e instanceof Array) {
return "array";
} else if (e instanceof Window) {
return "window";
} else if (e.nodeType) {
switch (e.nodeType) {
case 1:
return "element";
case 2:
return "attribute";
case 3:
return "text";
case 9:
return "document";
}
} else if (e.call) {
return "function";
}
} else if (typeof e === "function") {
if (e.call) {
return "function";
} else if (e instanceof RegExp) {
// V8
return "regexp";
}
}
return typeof e;
};
e.isArray
is a bit silly, tryArray.isArray(e)
\$\endgroup\$