On Jul 30, 2010, at 3:49 PM, Oliver Hunt wrote:
>>> By overloading for(in) we are effectively saying that there will never be a
>>> simple way to iterate arrays by value directly, because no one can even
>>> extend the builtin array type be have a generator for iteration because
>>> doing so would be too fragile.
>>
>> This "never be a simple way" is not true in JS1.7+:
>>
>> js> Array.prototype.__iterator__ = function () { for (let i = 0; i <
>> this.length; i++) yield this[i]; };
>> (function () {for (let i = 0; i < this.length; i++) {yield this[i];}})
>> js> for (v in [3,4,5]) print(v)
>> 3
>> 4
>> 5
>>
>> The unstratified, ugly-named __iterator__ meta-method is the getter or
>> factory for finding or creating an appropriate iterator. I use a generator,
>> since it is the simplest way of writing such a factory.
>
> I recognised that was possible -- the problem i was saying is that you can't
> do that due to it polluting the global array prototype in away that effects
> language semantics
Yeah, and it was hardly a "simple way" as I wrote it, but these little
headaches are fixable:
js> Object.defineProperty(Array.prototype, '__iterator__', {value: function ()
{ for (let i = 0; i < this.length; i++) yield this[i]; }});
[]
js> for (v in [3,4,5])print(v)
3
4
5
And put the Object.defineProperty call into some init-time code in JQuery ;-).
/be
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss