Skip to content

Instantly share code, notes, and snippets.

@sofish
Created October 1, 2012 13:15
Show Gist options
  • Save sofish/3811732 to your computer and use it in GitHub Desktop.
Save sofish/3811732 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.
@zieglar
Copy link

zieglar commented Oct 1, 2012

鱼大师!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment