判断数组

Array.isArray([]); // true

// 尝试用一个类似数组的对象去测试
Array.isArray({
    length: 1,
    "0": 1,
    slice: function() {}
}); // false

//如果你的开发环境不支持ECMAScript5,可以通过Object.prototype.toString()方法来代替。
if (typeof Array.isArray === "undefined") {
    Array.isArray = function(arg) {
        return Object.prototype.toString.call(arg) === "[object Array]";
    };
}

 

posted @ 2013-04-24 10:16  小猩猩君  阅读(146)  评论(0编辑  收藏  举报