Skip to curriculum
Jump to the editor

3.9. Array helpers on the prototype without breaking for…in

Medium
Topics
15–30 min

Add a helper with Array.prototype.last = function () { … } and every array has it. But run for (const key in [1, 2]) afterwards and you get "0", "1" and "last": the plain assignment made an enumerable property, and every for…in loop over an array in the page now sees it. Built-in methods like map never show up there, because they are installed differently. A careful helper library installs its methods the same way, and never replaces a method that already exists.

Write defineArrayMethod(name, fn) in array-helpers.js, then use it to give every array two methods, first() and last().

Signature

JAVASCRIPT
function defineArrayMethod(name, fn) // returns true or false
  • name: the method name, a string or a symbol.
  • fn: the method. Inside it, this is the array it is called on.
  • Returns true when it installed the method, false when arrays already had something by that name.

And, installed with defineArrayMethod:

  • first(): the item at index 0, or undefined for an empty array.
  • last(): the last item, or undefined for an empty array.

Requirements

  1. defineArrayMethod installs fn on Array.prototype, so every array can call it, with the array as this. It returns true.
  2. The method is not enumerable: for…in over an array lists only its indexes.
  3. Its property looks like a built-in method's: the value is fn itself, not enumerable, but writable and configurable, so it can be replaced or deleted later.
  4. Only Array.prototype changes: Object.prototype and plain objects are untouched.
  5. If arrays already have a property by that name, own or inherited (map, length, toString, hasOwnProperty, constructor, or a method installed earlier), nothing changes and it returns false.
  6. A symbol works as a name, with the same rules.
  7. It throws a TypeError when fn is not a function, or when name is not a string or a symbol, and installs nothing.
  8. first() and last() work as described above, and are installed through defineArrayMethod: after the file runs, Array.prototype has no enumerable properties.

Examples

JAVASCRIPT
defineArrayMethod("sum", function () {
  let total = 0;
  for (const value of this) total += value;
  return total;
});                            // true
[1, 2, 3].sum();               // 6
for (const key in [1, 2]) console.log(key); // "0", "1": no "sum"
JAVASCRIPT
defineArrayMethod("map", function () {});  // false: map is left alone
defineArrayMethod("sum", function () {});  // false: already installed
defineArrayMethod(5, function () {});      // TypeError
JAVASCRIPT
["a", "b", "c"].first();       // "a"
["a", "b", "c"].last();        // "c"
[].last();                     // undefined

Environment

  • Plain JavaScript in a browser page; the tests call the global defineArrayMethod, and first and last on arrays.
  • Every built-in is allowed. Making the methods work on array-like objects is out of scope.

Hints

  1. Which function lets you choose a property's flags, and what does it set the flags you leave out to?
  2. Which check finds a property whether it is on Array.prototype itself or inherited from Object.prototype?

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…