作者:小胖儿的大城 from: http://fins.iteye.com/blog/315643
从
http://blog.360.yahoo.com/blog-TBPekxc1dLNy5DOloPfzVvFIVOWMB0li?p=916
学来一招, 判断一个js对象是不是数组
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]'
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]'
本文介绍了一种可靠的JavaScript方法来判断一个对象是否为数组。通过使用Object.prototype.toString方法,并检查返回值是否为'[object Array]',可以准确地进行类型区分。
765

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



