DEV Community

Adam Crockett 🌀
Adam Crockett 🌀

Posted on

The Origin of Symbol.species

You may know I'm a big fan of very wired disgusting JavaScript which makes most feel a little nauseous.

Did you know you can mess with insuranceof? No I didn't either, this code is pushing the boundaries of good taste but I want you to know about it so here goes.

class MyArray extends Array {
  // Overwrite species to the parent Array constructor
  static get [Symbol.species]() { return Array; }
}
let a = new MyArray(1,2,3);
let mapped = a.map(x => x * x);

console.log(mapped instanceof MyArray); // false
console.log(mapped instanceof Array);   // true
Enter fullscreen mode Exit fullscreen mode

source: mdn

I read this and started laughing. Be careful it's a jungle out there.

Top comments (0)