在JavaScript中在数组上添加属性是一种好习惯吗 [英] Is it good practice to add properties on array in javascript

查看:59
本文介绍了在JavaScript中在数组上添加属性是一种好习惯吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数组是JavaScript中的对象,因此我们可以/应该在其上存储自定义属性吗?

Array is an Object in JavaScript so Can/Should we store custom properties on it ?

使用数组时,大多数时候我们需要维护数组的当前指针/当前索引.我看到了两种实现方法:

When we use an array, most of the times we need to maintain the current pointer/current index for the array. I see two implementations for that:

var myArray = [2, 4 ,6, 8, 10],
var myArray_top = 0;

实施2

var myArray = [2, 4 ,6, 8, 10],
myArray.top = 0; // Notice I have added top as property of array

#2的优点

  1. 我不需要为每个数组维护多个变量.
  2. 代码看起来更具可读性

#2的缺点

  1. 如果我将数组传递给方法,并且该方法可以操纵内容并返回带有内容的新数组.在这种情况下,我可能会丢失函数外部的"top"值.

这两种方法哪个更好?

推荐答案

一种反向观点.如果您具有与数组固有连接的值,并且希望轻松携带它们,那么我认为将它们作为属性放入数组是完全可以接受的,而且在许多情况下是理想的.

A contrarian view. If you have values which are intrinsically connected to the array, and you want to carry them around easily, I think it's completely acceptable, and in many cases desirable, to put them on the array as properties.

需要注意的一些事情,其中​​一些已经存在:

Some things to note, some of which you already have:

  • 如果您使用for...in循环遍历数组的元素,则将选择添加的属性,但是无论如何您都不会这样做,对吗?

  • If you're using for...in to loop over elements of the array, you'll pick up the added property, but you aren't doing that anyway, right?

您应该指出一个问题,例如slice这样的例程将返回没有添加属性的新数组.但是,执行类似切片的操作似乎也可能需要更改属性.因此,您可以编写自己的切片来保留/修改添加的属性.或者,您可以使用自变异数组函数,例如splice.

You are right to point out the issue that routines such as slice will return new arrays without the added property. However, it seems likely that doing something like a slice might require changing the property as well. So you could write your own slice which preserves/modifies the added property. Or, you could use self-mutating array functions such as splice.

您添加的属性将被JSON.stringify忽略.

Your added property will be ignored by JSON.stringify.

您可以将添加的属性视为一种元数据.将元数据直接存储在始终存在的对象上是很自然的.无论何时何地都必须在并行元数据对象周围徘徊,或者不得不将数组包装在一个对象中只是为了保留元数据,这似乎很奇怪.

You can think of your added property as a kind of metadata. It's natural to store metadata directly on the object, where it is always there. It seems odd to have to lug around a parallel metadata object wherever you go, or to have to wrap the array in an object just to keep metadata with it.

不管是好是坏,JS的设计人员都将数组实现为具有对象所有功能和机制(包括持有任意属性)的对象.当然,数组主要设计为具有自己的方法和自动length处理的数组.但这并没有理由放弃它们成为具有附加属性的对象的能力.

The designers of JS, for better or worse, implemented arrays as objects that have all the capabilities and machinery of objects, including holding arbitrary properties. Of course arrays are mainly designed to be arrays, with their own methods and automatic length handling. But that is no reason to deprecate their ability to also be objects with additional properties when that makes sense.

FWIW,Douglas Crockford在"JavaScript:The Good Parts"(第62页)中给出了一个向数组添加属性的示例,尽管在这种情况下,它是函数值的.

FWIW, Douglas Crockford in "JavaScript: The Good Parts" (p. 62) gives an example of adding a property to an array, although in his case it's function-valued.

当然,您应该确保设计时确实需要携带这些其他属性.担心和管理是另一回事.如果它们是可计算的,建议在需要时计算它们而不是存储它们.如果您将它们用作传递其他信息的简单方法,则可能需要重新访问您的设计.

Of course, you should make sure that your design is such that you really need to be carrying around these additional properties. It's another thing to worry about and manage. If they are computable, it may be advisable to compute them when needed rather than store them. If you're using them as a hacky way to pass around additional information, you may want to revisit your design.

说了这么多,有一个流派认为数组应该是数组.例如, Google编码指南

Having said all that, there is a school of thought that arrays should be arrays. For instance, Google coding guidelines say

不允许使用关联数组...或更准确地说,不允许您将非数字索引用于数组.在这种情况下,如果需要映射/哈希,请使用Object而不是Array,因为所需的功能实际上是Object的功能,而不是Array的功能.数组恰好扩展对象.

Associative Arrays are not allowed... or more precisely you are not allowed to use non number indexes for arrays. If you need a map/hash use Object instead of Array in these cases because the features that you want are actually features of Object and not of Array. Array just happens to extend Object.

因此,正如其他人在这里正确指出的那样,归根结底,归结为风格和偏好.

So at the end of the day, as others have correctly pointed out here, it boils down to style and preference.

这篇关于在JavaScript中在数组上添加属性是一种好习惯吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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