10.5. Negative indexes with a Proxy
Topics
This question is in
10. Proxies, iterators and generatorsIn JavaScript, list[-1] is not the last item: it reads a property named "-1", which almost never exists, so you get undefined. list.at(-1) reads from the end, but only for reading; there is no list.at(-1) = x. Other languages let a negative index count from the end everywhere, and a Proxy can give an array the same behavior for every kind of access, without changing the array.
Write withNegativeIndexes(array) in negative-index.js. It returns a Proxy around the array where a negative whole-number index -k means position length - k, for reading, writing, the in operator and delete. Everything else behaves as on the plain array.
Signature
function withNegativeIndexes(array) // returns the wrapped arrayarray: the array to wrap. It is not copied: changes through the wrapper show in the original, and the other way round.- Returns an array (
Array.isArrayistrue).
Requirements
- Positive indexes,
lengthand all array methods work as usual. - Reading
list[-k]gives the item atlength - k:list[-1]is the last item,list[-length]the first. A negative index before the start (-length - 1or lower), or any negative index on an empty array, givesundefined. - Writing
list[-k] = valuereplaces the item atlength - k, andlengthdoes not change. No property named"-1"is ever created on the array. - Writing a negative index before the start throws a
RangeError, and the array is unchanged. -k in lististruewhen positionlength - kholds an item, andfalsebefore the start or on a hole.delete list[-1]deletes the item at the last position, leaving a hole asdeleteon an array does. Before the start it does nothing and reports success.- The meaning is worked out at the moment of each access: after a
push,list[-1]is the new last item. - Only canonical negative whole numbers count from the end. Keys such as
"-1.5","-01"and"-x"are ordinary property names, as on a plain array.
Examples
const list = withNegativeIndexes(["a", "b", "c"]);
list[-1]; // "c"
list[-3]; // "a"
list[-4]; // undefined
list[0]; // "a": positive indexes are unchangedconst list = withNegativeIndexes([1, 2, 3]);
list[-1] = 30; // list is now [1, 2, 30], length still 3
list.push(4);
list[-1]; // 4
-4 in list; // true
-5 in list; // false
list[-9] = 0; // RangeErrorconst original = ["x", "y"];
const list = withNegativeIndexes(original);
delete list[-1];
original; // ["x", <hole>]: length is still 2
withNegativeIndexes([])[-1]; // undefinedEnvironment
- Plain JavaScript in a browser page; the tests call the global
withNegativeIndexesfromnegative-index.js. - Use a
Proxy. Do not add methods or properties to the array. Object.keys,for...inandObject.definePropertywith negative keys are not part of this problem.
Hints
- A trap receives the key as a string. How do you turn
"-2"into a position, and how do you reject"-01"? - Four operations are listed in requirements 2 to 6. Which trap handles each one?
The explanation comes with the course
A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.
- Every line explained
- The mistakes each test catches
- Links to the lessons behind it