Created
October 28, 2014 09:06
-
-
Save GZShi/4e3d97d2e41e1f5d4495 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 {} 待检测的对象 | |
* @return {object} | |
*/ | |
function type(obj) { | |
return { | |
'type': Object.prototype.toString.call(obj).substr(8).split(']')[0], | |
'toString': function () { return this.type; }, | |
'is': function (dest) { return dest === this.type; }, | |
'isNot': function (dest) { return dest !== this.type; } | |
}; | |
} | |
// usage | |
type('hello,world').is('String'); // true | |
type('hello,world').isNot('Array'); // true | |
type(/* nothing */).is('Undefined'); // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment