On Jun 20, 2012, at 9:37 PM, Erik Arvidsson wrote:

> In our previous discussion I had come to the understanding that the
> default iterator would use a private name. The main benefit for using
> a private name is to not pollute the property name space.
> 
> import iterator from '@iter';
> myObject[iterator] = function*() { ... };
> 
> If this was the ordinary property, "iterator", it might break existing
> code that already use "iterator" in some other way.

That was originally decided when we were using it to modify the behavior of 
for-in loops, but since then we decided to leave for-in alone and create for-of 
instead. This changes the cost trade-offs. Since for-of is a new thing under 
the sun, the idea is that you should only use it for objects that are known to 
be iterable -- because otherwise it throws. Objects are not iterable by default 
(in particular, Object.prototype.iterator doesn't exist).

So the chance/cost of potential name conflicts is much lower than it was when 
the iterator protocol extended for-in, because there it would cause existing 
loops, that weren't written with iterators in mind, to break.

> var obj = LibraryX.getObject();
> obj.iterator().forEach(...);

Maybe a better example is a non-thunk obj.iterator. But this is probably 
relatively rare, and you just wouldn't use obj in a for-of loop. You could 
either change the existing API or compatibly add a new iterable property like 
obj.elements.

> It also breaks objects as maps since any string can be used as key.

It doesn't really break, it just means you shouldn't use them as iterables. But 
keys(obj), vals(obj), items(obj) are better anyway since they let you pick 
which things you want to iterate over. Also Map.

Dave

_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to