On 16/05/2011 21:15, Jarek Foksa wrote:
The built-in typeof operator has some major screwups:
typeof null
"object"
typeof []
"object"
typeof /blah/
"function"
Here's the one I use:
// typeOf(null) == "null";
// typeOf(new String("string")) == "string";
// typeOf(document.attachEvent) == "function"; // MSIE
// typeOf(document.childNodes) == "object"; // Safari3-5
// typeOf(/regexp/) == "object"; // Firefox2-3.0/Chrome
function typeOf(object) {
var type = typeof object;
switch (type) {
case "object":
case "unknown":
return object == null
? "null"
: object.valueOf // JS object
? object.getUTCDay
? "object"
: typeof object.valueOf() // underlying type
// COM object (MSIE)
: /\[native code\]/.test(object) ? "function" : "object";
case "function":
if (!object.call) return "object";
}
return type;
}
I only use in it in edge cases. MOst of the time I'm happy to use the
standard typeof operator.
-dean
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]