Posted By

LucasRinaldi on 08/30/12


Tagged


Versions (?)

Checking Type of Object


 / Published in: JavaScript
 

'typeof' and 'instanceof' isn't a reliable method to find out the object class, so this is a function that does this in a more trustable way.

  1. function is(type, obj) {
  2. var clas = Object.prototype.toString.call(obj).slice(8, -1);
  3. return obj !== undefined && obj !== null && clas === type;
  4. }
  5.  
  6. is('String', 'test'); // true
  7. is('String', new String('test')); // true

Report this snippet  

You need to login to post a comment.