您的位置:首页 > 其它

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

2015-09-21 10:20 489 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: