some generator issues
Yuh-Ruey Chen
maian330 at gmail.com
Sun Apr 22 21:04:15 PDT 2007
Neil Mix wrote:
> >> Hard to standardize an argument; don't want another keyword.
> >> Suggestions?
> >>
> >> /be
> >>
> >
> > I was thinking of adding another property to the arguments object.
> > Perhaps, arguments.generator. The value for this property would be
> > null
> > (or undefined) if the function isn't a generator function.
>
> Something like this would be great. In practice, this particular
> suggestion might be a tad unwieldy:
>
> function foo() {
> var gen = arguments.generator;
> var command = new AsynchCommand();
> command.onResult = function(result) {
> gen.send(result);
> }
> command.execute();
> var result = yield;
> // do something with the result
> }
>
> The unwieldy part being that you're required to set
> arguments.generator to a local var because otherwise the arguments
> object gets masked. But this could be simplified a little if you
> could obtain a send() delegate directly from the generator object:
>
> function foo() {
> var command = new AsynchCommand();
> command.onResult = arguments.generator.delegate();
> command.start();
> var result = yield;
> // do something with the result
> }
>
Wouldn't Function.prototype.bind
(http://developer.mozilla.org/es4/proposals/static_generics.html) be
sufficient for this?
function foo() {
var command = new AsynchCommand();
command.onResult = arguments.generator.send.bind(arguments.generator);
command.execute();
var result = yield;
}
It's definitely wordier, but the user could also easily implement your
delegate() method above by adding it to Function.prototype.
Alternatively, we could make gen.next() and gen.send() special in that
they are already prebound to gen (e.g. gen.send.call(anotherGen) still
results in gen.send()). Personally, I'd rather not do this - pretty much
all builtin classes have unbound prototype methods rather than bound
instance methods.
-Yuh-Ruey Chen
More information about the Es4-discuss
mailing list