New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

%TypedArray%.of does not use @@species #1157

Closed
TimothyGu opened this Issue Mar 29, 2018 · 1 comment

Comments

Projects
None yet
3 participants
@TimothyGu
Member

TimothyGu commented Mar 29, 2018

%TypedArray%.of uses the this value as the constructor, not this[Symbol.species]. This seems inconsistent with the methods on %TypedArrayPrototype% like subarray and slice.

@allenwb

This comment has been minimized.

Show comment
Hide comment
@allenwb

allenwb Mar 29, 2018

Member

No, it's correct as written and handled exactly the same as in %Array%.of

of is a factory method of the constructor so the actual class constructor to be instantiated is the thisValue passed to the factory. What the algorithm is doing is approximately this:

class %TypedArray% {
   of(...args) {
      let ta = new this(args.length
      for (let i=0; ++1; i<args.length) ta[i]=args[i];
      return ta;
  }
}

When used in an expression referencing a subclass such as Int32Array.of(1,2,3) the value of this within the inherited method will be %Int32Array%. Essentially the expression is explicitly naming the kind of typed array it wants to instantiate.

@@species is primarily for use in instance methods that create derived collections whose class isn't explicit stated at the usage site.

Member

allenwb commented Mar 29, 2018

No, it's correct as written and handled exactly the same as in %Array%.of

of is a factory method of the constructor so the actual class constructor to be instantiated is the thisValue passed to the factory. What the algorithm is doing is approximately this:

class %TypedArray% {
   of(...args) {
      let ta = new this(args.length
      for (let i=0; ++1; i<args.length) ta[i]=args[i];
      return ta;
  }
}

When used in an expression referencing a subclass such as Int32Array.of(1,2,3) the value of this within the inherited method will be %Int32Array%. Essentially the expression is explicitly naming the kind of typed array it wants to instantiate.

@@species is primarily for use in instance methods that create derived collections whose class isn't explicit stated at the usage site.

@ljharb ljharb closed this Apr 1, 2018

@ljharb ljharb added the question label Apr 1, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment