Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Symbol species Property

JavaScript Symbol species Property

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.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments