Skip to content

Instantly share code, notes, and snippets.

@Yunkou
Last active September 8, 2017 20:05
Show Gist options
  • Save Yunkou/67d5da9d05b922479d771d8bcde3308d to your computer and use it in GitHub Desktop.
Save Yunkou/67d5da9d05b922479d771d8bcde3308d to your computer and use it in GitHub Desktop.
/* 检测对象类型
* @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