3.9. Array helpers on the prototype without breaking for…in
Topics
This question is in
3. Array methods from scratchAdd 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
function defineArrayMethod(name, fn) // returns true or falsename: the method name, a string or a symbol.fn: the method. Inside it,thisis the array it is called on.- Returns
truewhen it installed the method,falsewhen arrays already had something by that name.
And, installed with defineArrayMethod:
first(): the item at index 0, orundefinedfor an empty array.last(): the last item, orundefinedfor an empty array.
Requirements
defineArrayMethodinstallsfnonArray.prototype, so every array can call it, with the array asthis. It returnstrue.- The method is not enumerable:
for…inover an array lists only its indexes. - Its property looks like a built-in method's: the value is
fnitself, not enumerable, but writable and configurable, so it can be replaced or deleted later. - Only
Array.prototypechanges:Object.prototypeand plain objects are untouched. - 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 returnsfalse. - A symbol works as a name, with the same rules.
- It throws a
TypeErrorwhenfnis not a function, or whennameis not a string or a symbol, and installs nothing. first()andlast()work as described above, and are installed throughdefineArrayMethod: after the file runs,Array.prototypehas no enumerable properties.
Examples
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"defineArrayMethod("map", function () {}); // false: map is left alone
defineArrayMethod("sum", function () {}); // false: already installed
defineArrayMethod(5, function () {}); // TypeError["a", "b", "c"].first(); // "a"
["a", "b", "c"].last(); // "c"
[].last(); // undefinedEnvironment
- Plain JavaScript in a browser page; the tests call the global
defineArrayMethod, andfirstandlaston arrays. - Every built-in is allowed. Making the methods work on array-like objects is out of scope.
Hints
- Which function lets you choose a property's flags, and what does it set the flags you leave out to?
- Which check finds a property whether it is on
Array.prototypeitself or inherited fromObject.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