Search code examples
javascriptarraysconstructoriteratorarray.prototype.map

Does Array.map() use an iterator as constructor?


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.


Solution

  • This has nothing to do with iterators, it's because of the way that Array.prototype.map() creates the result.

    The result will be the same subclass as the object it's being called on, so it calls your constructor to create the result. It expects any subclasses of Array to support the same parameters as Array itself: either the argument is a single integer which is the size of the array to create, or it's multiple arguments to provide the initial contents of the array.

    Since the array you're mapping over is 3 elements long, it wants to create a Vector with 3 elements, so it calls Vector(3) to create this.

    You need to redefine your class to accept arguments like Array:

    class Vector extends Array {
      constructor(...args) {
        // Allow it to be called with an Array argument and convert it to Vector.
        if (args.length == 1 && Array.isArray(args[0])) {
          if (args[0].length == 1) {
            // need to handle 1 element specially so it's not used as the length
            super(1);
            self[0] = args[0];
          } else {
            super(...args[0]);
          }
        } else {
          super(...args);
        }
      }
    
      add(other) {
        return this.map((e, i) => e + other[i]);
      }
    }
    
    let a = new Vector([1, 2, 3]);
    console.log(a.add(a));

    This implementation accepts all the Array arguments, and additionally accepts an array as the sole argument in order to convert it to a vector.