为什么使用Javascript中通过本机阵列阵列状物体 [英] Why Are Array-Like Objects Used in Javascript Over Native Arrays

查看:153
本文介绍了为什么使用Javascript中通过本机阵列阵列状物体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在Javascript中非常普遍碰到类似数组的对象有一些相似之处建立数组类型,但没有它的所有方法或功能。正因如此,有一些技巧转换阵列状物体为真正的阵列以进一步处理。

It's very common in Javascript to come across Array-Like objects with some similarities to the build in Array type but without all of its methods or features. So much so that there are some tricks to convert Array-Like objects to "real" arrays for further manipulation.

这是即使在<一提到href=\"http://books.google.com/books?id=2weL0iAfrEMC&pg=PT144&lpg=PT144&dq=David+Flanagan+array-like&source=bl&ots=%5F8-ymy%5F45C&sig=I%5FQ6RRm%5FrnyI5%5FnzTFmDZVUkeBM&hl=en&ei=JC85S6mEK5D34Abp3qWqCA&sa=X&oi=book%5Fresult&ct=result&resnum=2&ved=0CA8Q6AEwAQ#v=onepage&q&f=false\">Javascript:权威指南。

该问题是为什么是这种模式如此普遍?为什么不preFER内置阵列式在所有这些情况?

The questions is why is this pattern so common? Why not prefer the built-in Array type in all these cases?

推荐答案

那么,谈论的核心的JavaScript对象,在参数是一个很好的例如说说。

Well, speaking about core Javascript objects, the arguments is a good example to talk about.

在此情况下,它一直是一个的阵列状物体的自年初以来,出现在ECMAScript中第1版规范已经作为一个简单的对象。

In this case it has been an array-like object since the beginning, appeared in the ECMAScript 1th Edition Specification already as a simple object.

为什么呢?我觉得在那个时候,只有四个内置的阵列方法,也许设计者没有想到它worthed太多,后来有人建议改变一下,但微软(该委员会TC39的一部分)没有批准的改变,怕的打破了网页的一贯present。

Why? I think at that time there were only four built-in Array methods, and maybe the designer didn't think it worthed too much, later the change was proposed, but Microsoft (part of the TC39 committee) didn't approved the change, the fear of breaking the web has always been present.

现在要去的主机对象的,DOM中,的NodeLists 来我的心,我认为他们没有想使用本机阵列键入由于这些对象的动态行为。

Now going to host objects, the DOM, NodeLists come to my mind, I think they didn't wanted to use the native Array type due the dynamic behavior of these objects.

的NodeLists 一般都是的生活的对象,它们的结构反映了底层DOM结构的任何变化...

NodeLists generally are live objects, their structure is reflects any change on the underlying DOM structure...

我个人喜欢阵列的对象,因为他们真的很轻巧,ECMAScript的前5名,有很多的核心方法的限制,对于用户自定义阵列状物体的使用。

Personally I like array-objects, because they are really lightweight, before ECMAScript 5, there were a lot of restrictions in core methods, regarding the usage of user-defined array-like objects.

例如,适用函数对象的方法,在ECMAScript中&LT; = 3,只允许任何一个真正的数组或参数对象作为第二个参数,现在在ES5,以下是可能的:

For example, the apply method of function objects, in ECMAScript <= 3, allowed only either a real array or an arguments object as its second argument, now in ES5, the following is possible:

var arrayLike = {0: 'hello ', 1:'world', length:2};
(function (a,b) { alert(a+b); }).apply(null, arrayLike);

另请参阅:

  • Why isn't a function's arguments object an array in Javascript?

这篇关于为什么使用Javascript中通过本机阵列阵列状物体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆