var is ={
types : ["Array", "Boolean", "Date", "Number", "Object", "RegExp", "String", "Window", "HTMLDocument"]
};
for(var i = 0, c; c = is.types[i ++ ]; ){
is[c] = (function(type){
return function(obj){
return Object.prototype.toString.call(obj) == "[object " + type + "]";
}
})(c);
}
alert(is.Array([])); // true
alert(is.Date(new Date)); // true
alert(is.RegExp(/\d/gi)); // true
本文介绍了一种使用JavaScript实现的类型检测方法。通过定义一个对象并利用`Object.prototype.toString.call`方法来判断变量的具体类型,包括基本的数据类型如数组、日期、正则表达式等。

被折叠的 条评论
为什么被折叠?



