2
\$\begingroup\$

By browser unknown, i mean i don't know how far back support goes for this.

Also, I'm wondering when I can delegate to typeof.

I've heard typeof is faster but the method below is more widely supported and also mentioned in ES5.

/*isType
**  dependencies - none
**  browser - unknwon
**
*/
NS.isType = function (type, o) {
    return (Object.prototype.toString.call(o).slice(8,1) === type);
};

/*getType
**  dependencies - none
**  browser - unknown
**
*/
NS.getType = function (o) {
    return (Object.prototype.toString.call(o).slice(8,1);
};

Clarification:

Not interested in detecting array like objects....just the language objects defined in ES 5.

\$\endgroup\$
1

2 Answers 2

4
\$\begingroup\$

How about:

NS.CheckType = function (o,test) {
    // implements both
    return test ? o.constructor.name === test : o.constructor.name;
};
// usage
NS.CheckType(false);       //=> 'Boolean'
NS.CheckType(false,Array); //=> false
NS.CheckType({},Object);   //=> true
NS.CheckType({},Array);    //=> false
NS.CheckType([],Object);   //=> false
NS.CheckType([],Array);    //=> true
NS.CheckType(/[a-z]/);     //=> 'RegExp'
NS.CheckType(0);           //=> 'Number'
// etc...

Because most js-things 'inherit' from Object you can also use:

Object.prototype.is = function (test) {
    return test ? this.constructor === test : this.constructor.name;
};
// usage
'string'.is();        //=> 'String'
'string'.is(Object);  //=> false
 (function(){}).is(); //=> Function
 var f = function(){};
 f.is(Function);      //=> true
 // also
 function Animal(name){this.name = name || 'some animal';}
 var dog = new Animal('Bello');
 dog.is(Animal); //=> true
 // etc...

[Edit] tested this in IE7-10:

Object.prototype.is = function (test) {
    return test 
      ? this.constructor === test 
      : (this.constructor.name || 
         String(this.constructor)
             .match ( /^function\s*([^\s(]+)/im)[1] );
};

for completeness: if the constructor function is anonymous the method will fail. Here's a solution for that:

Object.prototype.is = function (test) {
        return test 
          ? this.constructor === test 
          : (this.constructor.name || 
              ( String(this.constructor).match ( /^function\s*([^\s(]+)/im) 
               || ['','ANONYMOUS_CONSTRUCTOR'] ) [1] );
};
// usage
var Some = function(){ /* ... */}
    some = new Some;
some.is(); //=> 'ANONYMOUS_CONSTRUCTOR'

And as bonus:

Object.prototype.is = function() {
        var test = arguments.length ? [].slice.call(arguments) : null
           ,self = this.constructor;
        return test ? !!(test.filter(function(a){return a === self}).length)
               : (this.constructor.name ||
                  (String(self).match ( /^function\s*([^\s(]+)/im)
                    || [0,'ANONYMOUS_CONSTRUCTOR']) [1] );
}
// usage
var Some = function(){ /* ... */}
   ,Other = function(){ /* ... */}
   ,some = new Some;
2..is(String,Function,RegExp);        //=> false
2..is(String,Function,Number,RegExp); //=> true
some.is();                            //=> 'ANONYMOUS_CONSTRUCTOR'
some.is(Other);                       //=> false
some.is(Some);                        //=> true
// note: you can't use this for NaN (NaN === Number)
(+'ab2').is(Number);                 //=> true
\$\endgroup\$
4
  • \$\begingroup\$ Where can I find more information on constructor.name? Is it more or less compatible as Object.prototype.toString.call(o)? \$\endgroup\$
    – pure_code
    Commented Mar 2, 2013 at 22:16
  • \$\begingroup\$ I suppose its less compatible when it concerns IE. But well, everything is [less compatible when it concerns IE], right? Maybe, if it has to be IE-compatible, you should rewrite the method to use Object.prototype.toString.call(o) when constructor.name fails. Or just ditch IE-compatibility, that browser will never be up to standards. \$\endgroup\$
    – KooiInc
    Commented Mar 3, 2013 at 11:48
  • \$\begingroup\$ I've added a working IE tweak. The browser may not deserve it, the user does. \$\endgroup\$
    – KooiInc
    Commented Mar 3, 2013 at 23:41
  • \$\begingroup\$ Can you consolidate this into one or two methods. Check our underscorejs.org as one go-by. \$\endgroup\$
    – pure_code
    Commented Jun 4, 2013 at 20:57
1
\$\begingroup\$

I think you can run into issues when checking arrays across frames in IE 7 and older, so if you want to make it accurate for that case, check out this answer on StackOverflow.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.