简体   繁体   English

为什么在 Javascript 中使用类数组对象?

[英]Why use an array-like object in Javascript?

I get that since javascript allows numeric keys for objects, the existence of array-like objects is therefore technically possible, but why did they ever become common?我明白了,因为 javascript 允许对象使用数字键,因此类似数组的对象的存在在技术上是可能的,但为什么它们变得普遍?

Maybe the thought was that these array-like objects don't just have numeric keys, eg arguments has the callee property, so they can't be proper arrays to accommodate those properties.也许想法是这些类似数组的对象不仅有数字键,例如arguments具有callee属性,因此它们不能是容纳这些属性的正确数组。

But in javascript, it's perfectly valid to treat an array as an object and use non-numeric keys:但是在 javascript 中,将数组视为对象并使用非数字键是完全有效的:

var myArguments = []; 
myArguments[0] = 0; 
myArguments['callee'] = function(){console.log('callee')};

If the object is array-like and would benefit from having access to the functions it would otherwise inherit from the array prototype, what would be advantage of making it an array-like object instead?如果对象是类数组对象并且可以从访问它从数组原型继承的函数中受益,那么将其改为类数组对象有什么好处?


Edit: in case it wasn't clear in my question, an array like object would be something like the arguments object that has sequential numeric properties starting from zero, and has a length property that is one less than the highest numeric key.编辑:如果我的问题不清楚,像对象这样的数组将类似于arguments对象,它具有从零开始的顺序数字属性,并且具有比最高数字键小 1 的长度属性。 It also doesn't inherit from the array prototype, and doesn't provide access to array methods.它也不从数组原型继承,也不提供对数组方法的访问。

There is no big advantage, as you can access objects and arrays in very similar ways, array[number] for arrays, and object.propery , and object["property"] for objects.没有太大的优势,因为您可以以非常相似的方式访问对象和数组, array[number]用于数组, object.properyobject["property"]用于对象。 Arrays are better for storing data, not functions, in my opinion, and objects are better for storing sets of functions, in my opinion.在我看来,数组更适合存储数据,而不是函数,在我看来,对象更适合存储函数集。 For example, arrays could store your answers in a memory game, but an object would store the command for the game, like start or end.例如,数组可以将您的答案存储在记忆游戏中,但对象将存储游戏的命令,例如开始或结束。

I think the biggest advantage of using Array-Like object, is an object itself.我认为使用 Array-Like 对象的最大优点是对象本身。 An object has less overhead than an Array.对象的开销比数组少。 Can read: Array vs. Object efficiency in JavaScript可以阅读: JavaScript 中的数组与对象效率

The other advantage of using objects, Language does not have to create/allocate contiguous memory as in Array.使用对象的另一个优点是,语言不必像在数组中那样创建/分配连续的内存。 The object is more dynamic in nature.该对象本质上更具动态性。 The array has to create a memory block in advance.数组必须提前创建一个内存块。 and update on overload.并更新过载。

Read more: How are the JavaScript Arrays internally resizing?阅读更多: JavaScript 数组如何在内部调整大小?

Another factor, I think lookup in a map or object is faster.另一个因素,我认为在地图或对象中查找更快。

By array-like objects, I think you mean something like this:通过类似数组的对象,我认为您的意思是这样的:

const nativeObject = { 0: '@@Object', 1: 842713 };

One advantage would be the dynamic behaviour of the objects.优势之一是对象的动态行为。 For example, let's assume that you want to add new behaviour for your object.例如,假设您要为对象添加新行为。

 const nativeObject = { 0: '@@Object', 1: 842713, toString() { console.log(`[Name: ${this[0]}, Id: ${this[1]}]`); }, }; nativeObject.toString()

Overall objects offer high extendability and expressibility in the type of their keys compared to arrays.与数组相比,整体对象在其键的类型方面提供了高度的可扩展性和可表达性。

Actually Array in JS is an Object :)实际上JS中的数组是一个对象:)

As long as You need array, it is bad idea to create array-like objects, because JS engines optimize speed of arrays.只要你需要数组,创建类似数组的对象就不是一个好主意,因为 JS 引擎优化了数组的速度。 Yet, its possible.然而,它可能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM