Symbol.species Example From Mdn Not Making Sense?
Solution 1:
You are missing an important detail. If you subclass array, and then do a map
, using Symbol.species you can get back an array, not a member of your derived class. Sometimes you want that, for instance if you are exposing an API as a library author. You may want some special subclass sauce for your internal use but expose methods that return regular arrays for public consumption.
And there's no real limit, you can set up an alternate constructor for anything at all.
As for why commenting out the line doesn't change anything, remember that subclassing built-ins is new, may not be fully and correctly implemented, and that this is especially true with node.js where if they jump on the bandwagon early they get stuck supporting something 'wrong' for years in an LTS (they got burned on that on Object.observe).
Solution 2:
When I comment out the line
static get [Symbol.species]() { return Array; }
the result is exactly the same.
In a correct implementation of ES6 it wouldn't be the same!
For anyone looking for more examples, this article from Keith Cirkel (keithamus on Github) does a lot more at exlaining the concept than the MDN docs.
And here is an excerpt (emphasis mine):
"Now, if you were to make a
class Foo extends Array
- every time you calledFoo#map
while before it would return an Array (no fun) and you’d have to write your own Map implementation just to createFoo
s instead ofArray
s, nowFoo#map
return aFoo
, thanks toSymbol.species
"
Post a Comment for "Symbol.species Example From Mdn Not Making Sense?"