您的位置:首页 > Web前端 > JavaScript

判断JS对象是不是数组

2014-11-25 16:11 267 查看
function isArray(o) {
return Object.prototype.toString.call(o) === '[object Array]';
}


原文如下 :

引用

The Miller Device

The JavaScript language currently does not provide a good way to distinguish between objects and arrays. The typeof operator is broken: It identifies arrays as objects. Comparing a value's constructor property doesn't work because arrays created in a different
frame will have a different constructor. There are do-it-yourself tests for arrayness, but they are complicated and unreliable.

Mark Miller of The Google, by closely reading the ECMAScript standard, has discovered a simpler, more reliable test.

Object.prototype.toString.apply(value) === '[object Array]'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: