45
rated 0 times
[
49]
[
4]
/ answers: 1 / hits: 36171
/ 8 Years ago, mon, august 29, 2016, 12:00:00
Usually, I write code like this:
//definition
exports.getReply = function * (msg){
//...
return reply;
}
//usage
var msg = yield getReply ('hello');
but how can I write and use a generator in and out of an es6 class? I tried this:
class Reply{
*getReply (msg){
//...
return reply;
}
*otherFun(){
this.getReply(); //`this` seem to have no access to `getReply`
}
}
var Reply = new Reply();
Reply.getReply(); //out of class,how can I get access to `getReply`?
I also tried:
class Reply{
getReply(){
return function*(msg){
//...
return reply;
}
}
}
All of these two methods seem to be wrong answers. So how can I write generator functions in a class correctly?
More From » ecmascript-6