Skip to content

Instantly share code, notes, and snippets.

@NinoFocus
Last active July 19, 2019 15:53
Show Gist options
  • Save NinoFocus/4566248 to your computer and use it in GitHub Desktop.
Save NinoFocus/4566248 to your computer and use it in GitHub Desktop.
/**
* 判断对象的类型
* @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'
*/
@gterras
Copy link

gterras commented Jul 19, 2019

Very useful thanks

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