JavaScript Symbol.species Property
The Symbol.species property specifies a function-valued property that the constructor function uses to create derived objects. With this, we can create a derived object.
Syntax:
[Symbol.species]
Attribute: The species accessor property can be used to allow subclasses to override the default constructor for objects.
Example 1:
Javascript
class geek extends Array { static get [Symbol.species]() { return Array; } } const a = new geek(1, 2, 3, 4); const mapped = a.map((x) => 2); console.log(mapped instanceof geek); console.log(mapped instanceof geek); |
Output:
false false
Example 2:
Javascript
class geek extends Array { static get [Symbol.species]() { return Array; } } let a = new geek(1, 2, 3, 5, 7, 8); let mapped = a.map((x) => x); console.log(mapped instanceof geek); console.log(mapped instanceof Array); |
Output:
false true
We have a complete list of Javascript symbols, to check those please go through the Javascript Symbol Complete Reference article.
Please Login to comment...