Skip to content

Instantly share code, notes, and snippets.

@guxuerui
Created August 11, 2020 08:01
Show Gist options
  • Save guxuerui/8c14c5650d38996e54258da82ba4d4d4 to your computer and use it in GitHub Desktop.
Save guxuerui/8c14c5650d38996e54258da82ba4d4d4 to your computer and use it in GitHub Desktop.
JS正确判断数据类型
function isType(target, type) {
let targetType = Object.prototype.toString.call(target).slice(8, -1).toLowerCase()
return targetType === type.toLowerCase()
}
isType([], 'Array') // true
isType(/\d/, 'RegExp') // true
isType(new Date(), 'Date') // true
isType(function(){}, 'Function') // true
isType(Symbol(1), 'Symbol') // true
isType('1', 'String') // true
isType(1, 'Number') // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment