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

  

原文地址:https://www.cnblogs.com/ax-null/p/6774974.html