Created
August 11, 2020 08:01
-
-
Save guxuerui/8c14c5650d38996e54258da82ba4d4d4 to your computer and use it in GitHub Desktop.
JS正确判断数据类型
This file contains 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 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