Monday, April 1, 2013

Better way to get types of objects

  
    
function isDefined(m)  { return typeof m != 'undefined';   }
function isFunction(m) { return typeof m == 'function';    }
function isObject(m)   { return m && typeof m == 'object'; }
function isString(m)   { return typeof m == 'string';      }
function isBoolean(m)  { return typeof m == 'boolean';     }
function isNumber(m)   { return typeof m == 'number';      }
function isArray(m)    { return isObject(m) && Object.getPrototypeOf(m) == Array.prototype }

// For nodejs use this:

function isArray(m)    { return isObject(m) && m instanceof Array }
  

No comments:

Post a Comment