JavaScript中判斷對象類型

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章