Created
June 28, 2012 04:25
-
-
Save wiky/3009060 to your computer and use it in GitHub Desktop.
check the js variable type
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
(function(global) { | |
var type = (function() { | |
var cache = {}; | |
return function(obj) { | |
var key; | |
return obj === null ? 'null' // null | |
: obj === global ? 'global' // global | |
: (key = typeof obj) !== 'object' ? key // array, boolean, function, number, undefined | |
: obj.nodeType ? 'object' // DOM element | |
: cache[key = ({}).toString.call(obj)] // cached [object XXXX] | |
|| (cache[key] = key.slice(8, -1).toLowerCase()); // date, error, math, object, regexp | |
}; | |
}()); | |
if (typeof exports !== 'undefined') { | |
exports.type = type; | |
} else { | |
global.type = type; | |
} | |
}(this)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment