Created
October 1, 2012 13:15
-
-
Save sofish/3811732 to your computer and use it in GitHub Desktop.
detect object type in javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! 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
鱼大师!