js判断变量的类型(使用闭包来玩一把)
2021-07-05 00:03
标签:array tostring == ++ als ring iss i++ isarray js判断变量的类型(使用闭包来玩一把) 标签:array tostring == ++ als ring iss i++ isarray 原文地址:http://www.cnblogs.com/sorrowx/p/7109961.html var Type = (function() {
var Type = {};
for (var i = 0, type; type = [‘Undefined‘, ‘Null‘, ‘Boolean‘, ‘Number‘, ‘String‘, ‘Function‘, ‘Array‘, ‘Object‘][i++]; ) {
(function(type) {
Type[‘is‘ + type] = function(obj) {
return Object.prototype.toString.call(obj) === ‘[object ‘ + type + ‘]‘;
};
})(type);
};
return Type;
})();
console.log(Type.isUndefined()); // true
console.log(Type.isNull(a = null)); // true
console.log(Type.isBoolean(false)); // true
console.log(Type.isNumber(123)); // true
console.log(Type.isString(‘str‘)); // true
console.log(Type.isFunction(function() {})); // true
console.log(Type.isArray([1, 2, 3])); // true
console.log(Type.isObject({})); // true