JavaScript Symbol species Property
Last Updated :
19 May, 2023
JavaScript 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]
Property attributes: The species accessor property can be used to allow subclasses to override the default constructor for objects.
Return value: It returns a derived object.
Below examples illustrate the Symbol species property in JavaScript:
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
Supported Browsers: The browsers supported by JavaScript Symbol species property are listed below:
- Google Chrome 51
- Firefox 50
- Edge 15
- Opera
- Apple Safari
We have a complete list of Javascript symbols’ properties and methods, to check those please go through the Javascript Symbol Complete Reference article.
Share your thoughts in the comments
Please Login to comment...