Sub topic 14
Array-like receivers and why the result is always a plain Array
This chapter assumes you already know how toSorted, toSpliced, and with behave on ordinary arrays. You also need to know how Function.prototype.call sets this, and what an Array subclass created with class ... extends Array is.
The methods are generic
A generic array method makes few assumptions about its receiver, the value bound to this. The copying methods need only two things from the receiver: a length property and properties stored under index keys such as "0" and "1". Any object that has these is an array-like object. That includes arguments, NodeList, strings, and plain objects you build yourself.
const arrayLike = { length: 3, 0: "c", 1: "a", 2: "b" };
const sorted = Array.prototype.toSorted.call(arrayLike);
console.log(sorted); // ["a", "b", "c"]
console.log(Array.isArray(sorted)); // true
const replaced = Array.prototype.with.call(arrayLike, 0, "z");
console.log(replaced); // ["z", "a", "b"]
const spliced = Array.prototype.toSpliced.call(arrayLike, 1, 1);
console.log(spliced); // ["c", "b"]
console.log(arrayLike); // { '0': 'c', '1': 'a', '2': 'b', length: 3 } (unchanged)Strings work too, because the string is boxed into a String object that has a length and indexed characters:
console.log(Array.prototype.toSorted.call("cab")); // ["a", "b", "c"]
console.log(Array.prototype.toReversed.call("abc").join("")); // "cba"The mutating sort also accepts array-likes, but it writes back into the receiver. toSorted works on frozen or read-only array-likes such as strings because it never writes to them. Calling Array.prototype.sort.call("cab") throws a TypeError, because string indices cannot be assigned.
How the result is built
Each copying method follows the same steps internally:
The fourth step explains why gaps disappear. An array-like such as { length: 3, 0: "a" } gives ["a", undefined, undefined], which is a dense array.
No Symbol.species
Older copying methods such as map, filter, and slice create their result through Symbol.species. Because of that, a subclass instance returns another instance of the same subclass. The ES2023 methods skip this step and always create a plain Array:
class Tagged extends Array {
describe() {
return `Tagged(${this.length})`;
}
}
const list = Tagged.of(3, 1, 2);
console.log(list.slice() instanceof Tagged); // true (uses species)
console.log(list.toSorted() instanceof Tagged); // false (plain Array)
console.log(list.with(0, 9) instanceof Tagged); // false
console.log(typeof list.toSorted().describe); // "undefined"The committee made this choice on purpose. Species lookup is a common source of engine bugs and security problems, and it makes performance harder to predict. Dropping it keeps the new methods simple.
| Method | Uses `Symbol.species` | Result type for a subclass |
|---|---|---|
| `slice`, `map`, `filter` | Yes | The subclass |
| `toSorted`, `toSpliced`, `with`, `toReversed` | No | Plain `Array` |
Length limits
The result is a real Array, so its length cannot exceed 2^32 − 1. An array-like that reports a larger length causes a RangeError before any element is read. toSpliced also throws a TypeError if the computed new length would exceed Number.MAX_SAFE_INTEGER.
Exercise
What does Array.prototype.toSpliced.call({ length: 2, 0: "x" }, 1, 0, "y") return, and what is the type of the result?
Show answer
It returns ["x", "y", undefined]. The missing index 1 of the receiver is read as undefined and moved after the inserted "y". The result is a plain, dense Array, so Array.isArray returns true.
Exercise
Given class Stack extends Array {} and const s = Stack.of(1, 2), which of s.filter(() => true) and s.toReversed() is an instance of Stack, and why?
Show answer
Only s.filter(() => true). filter creates its result through Symbol.species, which resolves to Stack. toReversed always creates a plain Array and ignores species.