The following code is a javascript typeOf function. We aren't here to discuss coding conventions or whether or not extending a native object like I have is kosher or not. I want to know in what cases this would fail to correctly identify an object and hopefully ways to correct it. Or is this sufficient for 99% of cases?
Also, if you think of a way to make this smaller while keeping the information intact I'd be glad to hear that as well.
"use strict"
var FN = Function,
g = FN('return this')(); //jslint safe 'global' reference
Object.typeOf = (function (g) {
return function (o) {
var n = null,
r = false,
nodeType = {
1:'element_node',
2:'attribute_node',
3:'text_node',
4:'cdata_section_node',
5:'entity_reference_node',
6:'entity_node',
7:'processing_instrction_node',
8:'comment_node',
9:'document_node',
10:'document_type_node',
11:'document_fragment_node1',
12:'notation_node'
},
u;
/*********
* Following if statements are direct comparisons
* because any additional checks on o if it equals
* either of these results in an error. So we check
* these first prior to proceeding.
*********/
if (o === u) {
return 'undefined';
}
if (o === n) {
return 'null';
}
/*********
* If we made it this far, just return the set the
* r variable and return at the end;
*********/
switch (o) {
case g:
r = 'global';
break;
case g.document:
r = 'htmldocument';
break;
default:
break;
}
/*********
* Originally a large switch statement changed
* due to below comments.
*********/
if (o.nodeType && nodeType[o.nodeType]) {
r = nodeType[o.nodeType];
}
/*********
* If r is still false, we do a standard check
* using the Object.toString.call method to determine
* its "correct" type;
*********/
return r || ({}).toString.call(o).match(/\s([a-z|A-Z]+)/)[1].toLowerCase();
}
}(g));
if (o.nodeType && nodeType[o.nodeType]) { r = o.nodeType;}
, I think you mean to use:r = nodeType[o.nodeType]
\$\endgroup\$