Is it good practice to add array properties to javascript

An array is an object in JavaScript, so what can / should we store custom properties on it?

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

Implementation 1

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

      

Implementation 2

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

      

Pros # 2

  • I don't need to maintain multiple variables for each of my arrays.
  • The code looks more readable

Cons # 2

  • If you pass an array to a method and this method can manipulate the content and return a new array with the content. In this case, I can lose the value "from above" outside the function.

Which of the two is the best fit?

+3


source to share


4 answers


Contrasting performance. If you have values ​​that are essentially array bound and you want to transfer them easily, I think this is perfectly acceptable and in many cases desirable to put them in the array as properties.

Some notes, some of which you already have:

  • If you use for...in

    to iterate over the elements of an array, you get the added property, but you still don't, right?

  • You have the right to point out an issue in which procedures such as slice

    return new arrays with no property added. However, it seems likely that a property change might also be required to perform something like a slice. This way you can write your own snippet that saves / modifies the added property. Or, you can use self-tuning array functions like splice

    .

  • Your added property will be ignored JSON.stringify

    .

You can think of your added property as a kind of metadata. It is natural to store metadata directly on the object, where it is always present. It seems odd to have to concatenate a parallel metadata object wherever you go, or to wrap an array in an object so that only the metadata is stored.

JS designers, for better or worse, have implemented arrays as objects with all the capabilities and mechanisms of objects, including arbitrary properties. Of course, arrays are mostly meant for arrays, with their own methods and automatic handling length

. But this is not a reason to devalue their ability to also be objects with additional properties, if that makes sense.



FWIW, Douglas Crockford in JavaScript: Good Details (p. 62) gives an example of adding a property to an array, although in his case it is functionally meaningful.

Of course, you have to make sure your design is such that you really need to wear these extra properties. This is another thing to worry about and manage. If they are computable, it might be prudent to compute them as needed rather than store them. If you're using them as a hacky way to convey additional information, you might want to rethink your design.

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

Associative arrays are not allowed ... or, more precisely, you are not allowed to use unnumbered indices for arrays. If you need a map / hash, use Object instead of Array in these cases, because the functions you want are actually Object, not Array. Array just happens to extend Object.

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

+7


source


Consider a combination of both:

var myObj = {};
myObj.myArray = [2, 4 ,6, 8, 10];
myObj.top = 0;

      



Of course, if you try to keep everyone happy, you will not satisfy anyone.

+5


source


While this is mostly opinion based, you have more or less answered your own question.

The question is not how many variables you have, but instead you store them. Therefore, No. 2 1st professional is controversial. This leaves us with arguments on both sides: either a) make the code readable (again a matter of preference), or b) make the code function predictable.

It is pretty clear from this that you should choose predictability. Either use the first one or combine the two in one object.

+1


source


In javascript programming, primitive types are things like String, Boolean, and numeric types. Objects are "hash maps" that are stored as key / value pairs, while arrays are lists of any type stored in an index. Your second implementation confuses the line between arrays of objects by treating the array as an object. A better implementation would be to store your array as a property on this object. This code looks like this:

var myObject = {};
myObject.arrayValue = [2, 4, 6, 8, 10];
myObject.top = 0;

      

This makes it easier to iterate over the array values ​​and still bind other properties as part of that object. Hope this explains why this is the best sample. Good luck!

0


source







All Articles