10.2. An object that behaves like an array (push, pop, length)
Topics
This question is in
10. Proxies, iterators and generatorsAn array is mostly an ordinary object: its items are properties named "0", "1", "2", and length is one more number. What makes it an array is a little behavior on top: push writes at length and grows it, pop removes the last property, and making length smaller deletes everything past it. arguments, a NodeList and many library collections are array-like in exactly this sense, and the built-in array methods work on them because they only read length and indexes.
Write the class ArrayLike in array-like.js: a list that stores its items on itself, by index, and keeps length, push and pop honest, without being a real array.
Signature
class ArrayLike {
constructor(...items) // stores items at 0, 1, 2…
get length() // number of slots
set length(value) // shrinks or grows; RangeError for an invalid length
push(...items) // returns the new length
pop() // returns the removed item, or undefined
[Symbol.iterator]() // yields the items in index order
}Requirements
new ArrayLike()is empty, withlength0.new ArrayLike("a", "b")holds"a"at index 0 and"b"at index 1, and haslength2.- Each item is an own property of the object, at its index. The object is not a real array:
Array.isArrayisfalse(soextends Arrayis not allowed). push(...items)adds every item to the end in order and returns the newlength.push()with nothing returns the currentlength;push(undefined)still adds a slot.pop()removes the last item and returns it: afterwards its index is no longer a property (inisfalse) andlengthis one less. On an empty list,pop()returnsundefinedandlengthstays 0.Object.keysandfor...inlist only the indexes:lengthand the methods never show up.- Setting
lengthto a smaller number deletes every item at or past the new length. Setting it to a larger number adds empty slots (holes), as an array does;pop()on a hole returnsundefinedand still shrinks the list. lengthaccepts what an array'slengthaccepts: whole numbers from 0 to 2 ** 32 − 1, also as numeric strings ("2"). Any other number or string (-1,1.5,NaN,"abc") throws aRangeError, and a BigInt or a Symbol throws aTypeError, as for an array; either way the list is left unchanged.for...ofand spread give the items from index 0 tolength − 1.- Built-in array methods work on it through
call:Array.prototype.map.call(list, f),Array.prototype.join.call(list),Array.prototype.push.call(list, x),Array.from(list). - Every
ArrayLikehas its own items and length.
Examples
const list = new ArrayLike("a", "b");
list.push("c", "d"); // 4
list.pop(); // "d"
list.length; // 3
Object.keys(list); // ["0", "1", "2"]
[...list]; // ["a", "b", "c"]const list = new ArrayLike(1, 2, 3, 4);
list.length = 2;
2 in list; // false: the items past the new length are gone
list.length = 4;
[...list]; // [1, 2, undefined, undefined]: two holes
list.length = -1; // RangeErrorconst empty = new ArrayLike();
empty.pop(); // undefined
empty.length; // 0
Array.prototype.join.call(new ArrayLike("x", "y"), "-"); // "x-y"Environment
- Plain JavaScript in a browser page; the tests use the global class
ArrayLikefromarray-like.js. - Do not extend
Array. Writing an index directly (list[7] = "x") is not expected to changelength: that needs a Proxy, which is a different problem. - Other array methods (
shift,splice,at) are not part of this problem; the built-ins work throughcall.
Hints
- Where can
lengthlive so that it is readable aslist.lengthbut never shows up inObject.keys? - When the list shrinks, which properties have to go, and in which order is it easiest to remove them?
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