js 判断类型

var type = (function() {
		var getType = function(o) { return Object.prototype.toString.call(o) };

		return {
			isNumber: function(o) { return getType(o)    === '[object Number]'; },
			isBoolean: function(o) { return getType(o)   === '[object Boolean]';},
			isString: function(o) {return getType(o)     === '[object String]';},
			isFunction: function(o) {return getType(o)   === '[object Function]';},
			isArray: function(o) { return getType(o)     === '[object Array]';},
			isObject: function(o) { return getType(o)    === '[object Object]';},
			isDate: function(o) { return getType(o)      === '[object Date]';},
			isRegExp: function(o) { return getType(o)    === '[object RegExp]';},
			isNull: function(o) { return getType(o)      === '[object Null]';},
			isUndefined: function(o) { return getType(o) === '[object Undefined]'; }
		};
}());


// test 
var n = 2;
var s = "hello";
console.log(type.isNumber(n), type.isString(s));   // true true

  

posted @ 2017-04-27 16:37  ax=null  阅读(655)  评论(0编辑  收藏  举报