1

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.

2

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

It should also be noted that super(... array) is problematic when the Vector constructor is passed [2] for example, because then the Array constructor will think it's supposed to create a new array of length 2.
@Kaiido How stupid of me, of course!
@Pointy That's what all the if conditions were supposed to handle, but I left out the original spread syntax.
@Kaiido And that fixed the "non-callable @@iterator" error.
Yes. Part of the problem is that I originally wrote constructor(size=0, ...args). When I changed the function signature I didn't fix up all the uses properly.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.