Skip to curriculum
Jump to the editor

10.2. An object that behaves like an array (push, pop, length)

Medium
Topics
15–30 min

An 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

JAVASCRIPT
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

  1. new ArrayLike() is empty, with length 0. new ArrayLike("a", "b") holds "a" at index 0 and "b" at index 1, and has length 2.
  2. Each item is an own property of the object, at its index. The object is not a real array: Array.isArray is false (so extends Array is not allowed).
  3. push(...items) adds every item to the end in order and returns the new length. push() with nothing returns the current length; push(undefined) still adds a slot.
  4. pop() removes the last item and returns it: afterwards its index is no longer a property (in is false) and length is one less. On an empty list, pop() returns undefined and length stays 0.
  5. Object.keys and for...in list only the indexes: length and the methods never show up.
  6. Setting length to 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 returns undefined and still shrinks the list.
  7. length accepts what an array's length accepts: whole numbers from 0 to 2 ** 32 − 1, also as numeric strings ("2"). Any other number or string (-1, 1.5, NaN, "abc") throws a RangeError, and a BigInt or a Symbol throws a TypeError, as for an array; either way the list is left unchanged.
  8. for...of and spread give the items from index 0 to length − 1.
  9. 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).
  10. Every ArrayLike has its own items and length.

Examples

JAVASCRIPT
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"]
JAVASCRIPT
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;      // RangeError
JAVASCRIPT
const 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 ArrayLike from array-like.js.
  • Do not extend Array. Writing an index directly (list[7] = "x") is not expected to change length: 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 through call.

Hints

  1. Where can length live so that it is readable as list.length but never shows up in Object.keys?
  2. 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
Back to the question
Loading the editor…