caitp / watchtower.js-old

MOVED TO https://github.com/angular/watchtower.js
6 stars 3 forks source link

non-Array Iterables: how to detect, and how to mock nicely for tests #10

Open caitp opened 10 years ago

caitp commented 10 years ago

Recently, a test was commented out due to testing a behaviour which is not strictly present in ES6, the Dart Iterable protocol.

However, ES6 does have an idea of iterators which enable for X in OBJ expressions, and these depend on the OBJ.prototype.iterator() method being available. "Iterator" objects in ES6 are the product of the "iterator()" method, and contain IT.prototype.next() (according to http://wiki.ecmascript.org/doku.php?id=harmony:iterators)

It would be good to figure out a "good" way to determine if an object is Iterable (so that there are fewer blocks of code which are not being used). My feeling is that just typeof Object.getPrototypeOf(OBJ).iterator === "function" would be good enough. But I'm not totally sure, it's possible that this only works when an object is well-behaved.

Anyways, find a way to deal with this, and mock it in a simple fashion and use it in the test, so that we can uncomment the test.

caitp commented 10 years ago

I've asked on es-discuss, and the suggested approach is to use Symbol.iterator in OBJ. I don't really like this approach, particularly when a native implementation isn't being used, because the use of this in an inner loop seems possibly fishy.

But, the shim does seem to provide this mechanism, so maybe it's good enough for now.

caitp commented 10 years ago

I'm still not totally sure how this is supposed to work with Traceur-compiled ES6, this seems pretty hopeless until browsers can support this properly. Otherwise it's simply too much code to bother. I'm going to put this on hold for now...