paulmillr / es6-shim

ECMAScript 6 compatibility shims for legacy JS engines
http://paulmillr.com
MIT License
3.11k stars 387 forks source link

Using more widely used symbol polyfill or means of detecting `$iterator$` #427

Closed benlesh closed 7 years ago

benlesh commented 7 years ago

Currently this library uses a library-specific string for iterators if Symbol.iterator does not exist.

This has meant we need a strange workaround for RxJS to access iterators on types provided by es6-shim. This isn't ideal, because if you change your implementation of Set or Map, our workaround will break.

There are two fixes I'll propose for this, and neither are perfect:

  1. Use a more common key for the iterator property
    • Basically use "@@iterator".
  2. Publish the $iterator$ variable you're using somewhere globally so other libraries like RxJS can check for it and use it without workarounds.
ljharb commented 7 years ago

When we're in a browser that has a native property - like certain versions of Firefox with "@@iterator" - we use that. However, when we're not in a browser that has that, or Symbol, there's no global place to put it.

Where would you suggest?

Note that the "strange workaround" you're using is actually the perfect and correct approach to reliably get this value - I'm not sure why you'd need a different one.

benlesh commented 7 years ago

Note that the "strange workaround" you're using is actually the perfect and correct approach to reliably get this value - I'm not sure why you'd need a different one.

If you ever change your implementation of Map or Set, such that Map.prototype[$yourIterator$] !== Map.prototype.entries, then the workaround breaks.

ljharb commented 7 years ago

@blesh if we did that, we'd be violating the spec - thus, we will never do that.

ljharb commented 7 years ago

To clarify, the spec requires that Map.prototype[Symbol.iterator] === Map.prototype.entries && Set.prototype[Symbol.iterator] === Set.prototype.values && Array.prototype[Symbol.iterator] === Array.prototype.values be true.

benlesh commented 7 years ago

Interesting. Thanks. I couldn't locate the spec on this.

It's still unfortunate that we have a special case for es6-shim due to a somewhat avant-garde symbol property name. (@@iterator would have been nice)

Thanks for your feedback.

ljharb commented 7 years ago

I explicitly chose not to use "@@iterator" to avoid people wrongly attempting to "polyfill" iterators based on Firefox's unfortunate choice to make a nonstandard string property magical.

benlesh commented 7 years ago

I think we all would have been better off if people had just stuck to a few agreed upon conventions, honestly. But it's no big deal. It'll all be gone in a few years.

ljharb commented 7 years ago

I love your optimism :-) the first browser that didn't need the es5-shim appeared within the last year, 6 years after ES5 was standard.

benlesh commented 7 years ago

Haha... Aw, 6 years isn't a big deal. Look how long it took Java to get generics or arrow functions, and that's just one platform.