Last active
July 19, 2019 15:53
-
-
Save NinoFocus/4566248 to your computer and use it in GitHub Desktop.
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
/** | |
* 判断对象的类型 | |
* @param {Object} obj | |
* @return {String} | |
*/ | |
function $typeof(obj) { | |
return Object.prototype.toString.call(obj).replace(/^\[object |\]$/g,'').toLowerCase(); | |
} | |
/* Usage: | |
$typeof({}) #=> 'object' | |
$typeof([]) #=> 'array' | |
$typeof('') #=> 'string' | |
$typeof(1) #=> 'number' | |
$typeof(true) #=> 'boolean' | |
$typeof(null) #=> 'null' | |
$typeof(undefined) #=> 'undefined' | |
$typeof(function() {}) #=> 'function' | |
$typeof(new Map()) #=> 'map' | |
$typeof(new WeakMap()) #=> 'weakmap' | |
$typeof(new Set()) #=> 'set' | |
$typeof(new WeakSet()) #=> 'weakset' | |
$typeof(Symbol('1')) #=> 'symbol' | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very useful thanks