This is normally a detail that is hidden from the programmer, but as a test I made a new class Vector extending Array that changed the constructor to unpack an input array and now Vector.map()
stopped working:
class Vector extends Array {
constructor(array) {
console.assert(Array.isArray(array),
`Vector constructor expected Array, got ${array}`);
// ignore special case of length 1 array
super(...array);
}
add(other) {
return this.map((e, i) => e + other[i]);
}
}
let a = new Vector([1,2,3]);
console.log(a.add(a));
I assume what happened based on my Python experience is that Array.map()
internally constructs a new Array with an iterator, like that of Array.prototype[@@iterator]()
, but my Vector constructor instead takes an Array object. Is this correct? Maybe map is a primitive of the JS implementation.