// Hypothetical underlying implementation for illustration
class TypedArray {
static get [Symbol.species]() {
return this;
}
}TypedArray[Symbol.species] / notebook
TypedArray[Symbol.species]
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
class SubTypedArray extends Int8Array {}
SubTypedArray[Symbol.species] === SubTypedArray; // trueclass BadArray extends Int8Array {
static get [Symbol.species]() {
return Array;
}
}
new BadArray(1).map(() => 0); // TypeError: Method %TypedArray%.prototype.map called on incompatible receiver [object Array]
class BadArray2 extends Int8Array {
static get [Symbol.species]() {
return BigInt64Array;
}
}
new BadArray2(1).map(() => 0n); // TypeError: TypedArray.prototype.map constructed typed array of different content type from |this|Int8Array[Symbol.species]; // function Int8Array()
Uint8Array[Symbol.species]; // function Uint8Array()
Float32Array[Symbol.species]; // function Float32Array()class MyTypedArray extends Uint8Array {
// Overwrite MyTypedArray species to the parent Uint8Array constructor
static get [Symbol.species]() {
return Uint8Array;
}
}