函数作为返回值输出 判断数据类型

1判断数据类型

var gettype = function(type){
    return function(obj){
        return Object.prototype.toString.call(obj) == '[object '+ type +']';
    }
}

var isString = gettype('String');
console.log(isString('123'));//true

应用循环批量注册isType函数

var Type={};
for(var i=0,type;type=['String','Array','Number'][i++];){
    (function(type){
        Type['is' + type] = function(obj){
            return Object.prototype.toString.call(obj) === '[object '+ type +']';
        }
    })(type);
}

console.log(Type.isArray( [] ));//true
原文地址:https://www.cnblogs.com/junwu/p/4825372.html