What does Arrayzing provide that a normal Array doesn't? I hope to answer that question in my series of posts Arrayzing vs. Array. In today's installment, we'll take a look at what the get() and set() functions offer over an Array's traditional [] accessor.

When using plain old JavaScript Array objects, a common operation is to access or modify elements. For example:

var a = [1, 2, 3];
var sum = a[0] + a[1] + a[2]; // = 6
a[0] = 6;
a.join(); // "6,2,3"

Arrayzing also supports the built-in [] accessor. However, it should be noted there are limitations to accessing an Array in this manner, most notable amongst them being that we must always index from the left. For example, imagine we wanted to access the last element. Using a normal Array, we would have to do this:

var a = [1, 2, 3];
a[a.length - 1] = 4; // a = [1, 2, 4];
alert(a[a.length - 1]); // shows 4

Although this works, it's not the most convenient or elegant way to do it. To address this problem, Arrayzing provides two new functions get() and set(). These functions act essentially the same as the built-in [] accessors with an added benefit: we can index from the left and the right. For example, to implement the above example using Arrayzing, we could instead do this:

var a = _(1, 2, 3);
a.set$(-1, 4); // a = [1, 2, 4];
alert(a.get(-1)); // shows 4

(Aside: If you're not familiar with the $ after set$(), check out this article. It means that the function is a mutator).

What's more, all Arrayzing commands that take an index as an argument allow right-indexing. What convenience!

There is one further advantage of using set(): we can change an element in your zing (a zing is an Arrayzing array) without modifying the original array. For example, if we wanted to replace an element in our zing but didn't want to modify the original array, we would do this:

var a = _(1, 2, 3);
var b = a.set(0, 10); // not set$()
a.str(); // returns "1,2,3"
b.str(); // returns "10,2,3"

As you can see, Arrayzing provides a strict superset of the capabilities of a normal Array's accessors, allowing you to write cleaner, easier code.