Skip to content

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

Closed

Description

@TimothyGu

%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.

Activity

  1. allenwb commented on Mar 29, 2018

    @allenwb
    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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions