paulmillr / es6-shim

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

Expose Symbol.iterator #360

Closed marclaval closed 8 years ago

marclaval commented 8 years ago

Hello,

it can be very nice to use Symbol.iterator. For example: https://github.com/angular/angular/blob/master/modules/angular2/src/core/compiler/query_list.ts#L104 But the shim doesn't expose '_es6-shim iterator_' as Symbol.iterator.

Cheers

cscott commented 8 years ago

The shim doesn't expose features which can't be 100% implemented in es5 browsers, thus it doesn't define Symbol at all. It might be a reasonable feature to add to es6-sham, though.

ljharb commented 8 years ago

Under no circumstances will es6-shim expose Symbol or anything underneath it, since Symbols are simply impossible to faithfully shim.

You can derive es6-shim's internal Symbol.iterator value by looking in Object.getOwnPropertyNames(Map.prototype), and finding the key (besides "entries" itself, and "size" which throws on access) whose value is === Map.prototype.entries. Namely, Object.getOwnPropertyNames(Map.prototype).find(function (key) { return key !== 'entries' && key !== 'size' && Map.prototype[key] === Map.prototype.entries; });

I wouldn't accept this in es6-sham by itself, however, since the existence of Symbol in the global space could break a lot of code that doesn't properly check Symbol support.

marclaval commented 8 years ago

@ljharb fair enough. Thanks for the tip to retrieve the internal value.

ljharb commented 4 years ago

This detection is now unnecessary; https://npmjs.com/es-get-iterator will let you get an iterator from any value.