Skip to content

Instantly share code, notes, and snippets.

@GZShi
Created October 28, 2014 09:06
Show Gist options
  • Save GZShi/4e3d97d2e41e1f5d4495 to your computer and use it in GitHub Desktop.
Save GZShi/4e3d97d2e41e1f5d4495 to your computer and use it in GitHub Desktop.
简单的类型检测
/**
* 类型检测
*
* @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