Skip to content

Instantly share code, notes, and snippets.

@zhouwei421
Forked from sofish/typeof.js
Created October 2, 2012 10:22
Show Gist options
  • Select an option

  • Save zhouwei421/3818047 to your computer and use it in GitHub Desktop.

Select an option

Save zhouwei421/3818047 to your computer and use it in GitHub Desktop.
detect object type in javascript
/*! copyright: sofish, https://github.com/sofish, licensed under MIT */
var util = {};
// detect object type
// note that everything is a object in javascript
util.type = function(obj) {
var str = Object.prototype.toString.call(obj);
return str.replace(/^\[object (\w+)\]$/, '$1');
}
// test case
util.type('String') === 'String';
util.type(['Array']) === 'Array';
util.type({type: 'Object'}) === 'Object';
util.type(1) === 'Number';
// etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment