Last active
September 8, 2017 20:05
-
-
Save Yunkou/67d5da9d05b922479d771d8bcde3308d to your computer and use it in GitHub Desktop.
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
/* 检测对象类型 | |
* @param: obj {JavaScript Object} | |
* @param: typeStr {String} 以大写开头的 JS 类型名 | |
* @return: {Boolean} | |
*/ | |
function isType(obj, typeStr) { | |
return Object.prototype.toString.call(obj).slice(8, -1) === typeStr; | |
} | |
// all is true | |
isType('Hello World', 'String') | |
isType(0, 'Number') | |
isType([], 'Array') | |
isType({}, 'Object') | |
isType(undefined, 'Undefined') | |
isType(false, 'Boolean') | |
isType(null, 'Null') | |
isType(new FormData(), 'FormData') | |
isType(new ArrayBuffer(10), 'ArrayBuffer') | |
isType(new Date(), 'Date') | |
isType(new RegExp(), 'RegExp') | |
// ... whatever you want |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment