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?
question from:
https://stackoverflow.com/questions/39197811/how-can-i-write-a-generator-in-a-javascript-class 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…