JS Built-ins
← Array10 / 226
ArrayArray[Symbol.species]advanced

Array[Symbol.species] β€” subclass concat()

πŸ“– Usage Example
class MyArr extends Array {}
const a = new MyArr(1, 2)
const b = a.concat([3, 4])
b instanceof MyArr  // β†’ true

concat() also respects Symbol.species when called on an Array subclass instance.

Array[Symbol.species] and concat()

The .concat() method also honours Symbol.species. When called on an Array subclass (and no species override is present), it returns an instance of the subclass.

Challenge: Create ConcatArray that extends Array. Implement concatReturnsSubclass that returns true when .concat() on a ConcatArray instance produces another ConcatArray instance.

concatReturnsSubclass() // β†’ true
πŸ’‘ Hints (2)
  • β€’ `.concat()` creates a new array via `Symbol.species`, just like `.map()` and `.filter()`.
  • β€’ No override is needed β€” subclasses inherit this behaviour automatically.