`
lijackly
  • 浏览: 71256 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

新学一招, 判断一个js对象是不是数组

阅读更多

大城小胖的大城【永久域名 http://fins.iteye.com】处转帖:


http://blog.360.yahoo.com/blog-TBPekxc1dLNy5DOloPfzVvFIVOWMB0li?p=916
学来一招, 判断一个js对象是不是数组


Java代码
引用

   1. function isArray(o) { 
   2.   return Object.prototype.toString.call(o) === '[object Array]';  
   3. } 

 


原文如下 :

引用

  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]'



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics