如何在JavaScript类中编写生成器?
通常,我编写的代码如下:如何在JavaScript类中编写生成器?,javascript,ecmascript-6,Javascript,Ecmascript 6,通常,我编写的代码如下: //definition exports.getReply = function * (msg){ //... return reply; } //usage var msg = yield getReply ('hello'); class Reply{ *getReply (msg){ // yield something here, otherwise you should use a normal function
//definition
exports.getReply = function * (msg){
//...
return reply;
}
//usage
var msg = yield getReply ('hello');
class Reply{
*getReply (msg){
// yield something here, otherwise you should use a normal function
return reply;
}
*otherFun(){
const reply = yield this.getReply(); // yield the generator so it does what it needs to and doesn't wait for the .next() to be called on it
return `${reply} within class ${this.constructor.name}`;
}
}
const reply = new Reply();
const answer = yield reply.getReply('foo');
// getReply is a generator function, so it needs a `yield` or `.next()` to run beyond the first `yield` in the function
但是如何在es6类中编写和使用生成器呢?我试过这个:
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`?
我还尝试:
class Reply{
getReply(){
return function*(msg){
//...
return reply;
}
}
}
这两种方法似乎都是错误的答案。那么,我如何才能在类中正确地编写生成器函数呢?生成器是带有
.next()
的函数,用于获取收益率
'ed值,或者您可以收益率
生成器函数,让它知道在遇到收益率
语句时无需“等待”即可调用.next
(reply.getReply().next(fn)
)
您的第二段代码几乎是正确的:
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`?
首先,在ES6中工作时,请使用const
或let
,并且仅对类使用大写变量。
您正试图用var Reply=
语句覆盖class Reply
语句,这是不可能的,因为已经声明了标识符“Reply”
您正在寻找的答案如下:就像在第一个示例中所做的那样,您应该
产生
生成器函数,因此您的函数应该如下所示:
//definition
exports.getReply = function * (msg){
//...
return reply;
}
//usage
var msg = yield getReply ('hello');
class Reply{
*getReply (msg){
// yield something here, otherwise you should use a normal function
return reply;
}
*otherFun(){
const reply = yield this.getReply(); // yield the generator so it does what it needs to and doesn't wait for the .next() to be called on it
return `${reply} within class ${this.constructor.name}`;
}
}
const reply = new Reply();
const answer = yield reply.getReply('foo');
// getReply is a generator function, so it needs a `yield` or `.next()` to run beyond the first `yield` in the function
生成器是带有
.next()
的函数,用于获取收益率
'ed值,或者您可以收益率
生成器函数,让它在遇到收益率
语句时不需要“等待”调用.next
(reply.getReply().next(fn)
)
您的第二段代码几乎是正确的:
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`?
首先,在ES6中工作时,请使用const
或let
,并且仅对类使用大写变量。
您正试图用var Reply=
语句覆盖class Reply
语句,这是不可能的,因为已经声明了标识符“Reply”
您正在寻找的答案如下:就像在第一个示例中所做的那样,您应该
产生
生成器函数,因此您的函数应该如下所示:
//definition
exports.getReply = function * (msg){
//...
return reply;
}
//usage
var msg = yield getReply ('hello');
class Reply{
*getReply (msg){
// yield something here, otherwise you should use a normal function
return reply;
}
*otherFun(){
const reply = yield this.getReply(); // yield the generator so it does what it needs to and doesn't wait for the .next() to be called on it
return `${reply} within class ${this.constructor.name}`;
}
}
const reply = new Reply();
const answer = yield reply.getReply('foo');
// getReply is a generator function, so it needs a `yield` or `.next()` to run beyond the first `yield` in the function
编辑:添加更多示例。
您的
class
定义(几乎)是正确的。错误发生在实例化var Reply=new Reply();
中。这试图重新定义分配给类名的变量。另外generator
函数应该产生一些东西。我详细阐述了一些操作代码来展示工作示例
类回复{
//添加用于测试目的
构造函数(…参数){
this.args=args;
}
*getReply(msg){
for(让arg在这个.args中){
让reply=msg+this.args[arg];
//发电机应该产生一些东西
作出答复;
}
//下一个调用返回(产生){done:true,value:undefined}
}
*其他乐趣(){
产生这个。getReply('很高兴认识你');//产生生成器对象
产生这个.getReply('See you');//是的,这个可以访问
//下一个调用产生{done:true,value:undefined}
}
*伊文莫尔(){
yield*this.getReply('I miss you');//产生生成器结果
收益*this.getReply('我更想念你');
}
}
//现在测试一下我们有什么
const reply=新回复(“彼得”、“詹姆斯”、“约翰”);
//由于全局范围的原因,这里的let和var是可互换的
var r=reply.getReply('Hello');
var msg=r.next();//{done:false,值:“…”}
而(!msg.done){
console.log(msg.value);
msg=r.next();
}
var other=reply.otherFun();
var o=other.next();//{done:false,value:Generator}
而(!o.done){
设gen=o.value;
msg=下一代();
而(!msg.done){
console.log(msg.value);
msg=下一代();
}
o=其他。下一步();
}
var more=reply.evenMore();
msg=more.next();
而(!msg.done){
console.log(msg.value);
msg=more.next();
}
//2019年1月12日更新
//更多例子
for(让r of reply.getReply('for')){
控制台日志(r);
}
for(让r表示答复。evenMore()){
控制台日志(r);
}
//请注意,由于发电机功能中缺少星号(*),以下操作不起作用
for(让r表示reply.otherFun()){
控制台日志(r);
}
编辑:添加更多示例。
您的class
定义(几乎)是正确的。错误发生在实例化var Reply=new Reply();
中。这试图重新定义分配给类名的变量。另外generator
函数应该产生一些东西。我详细阐述了一些操作代码来展示工作示例
类回复{
//添加用于测试目的
构造函数(…参数){
this.args=args;
}
*getReply(msg){
for(让arg在这个.args中){
让reply=msg+this.args[arg];
//发电机应该产生一些东西
作出答复;
}
//下一个调用返回(产生){done:true,value:undefined}
}
*其他乐趣(){
产生这个。getReply('很高兴认识你');//产生生成器对象
产生这个.getReply('See you');//是的,这个可以访问
//下一个调用产生{done:true,value:undefined}
}
*伊文莫尔(){
yield*this.getReply('I miss you');//产生生成器结果
收益*this.getReply('我更想念你');
}
}
//现在测试一下我们有什么
const reply=新回复(“彼得”、“詹姆斯”、“约翰”);
//由于全局范围的原因,这里的let和var是可互换的
var r=reply.getReply('Hello');
var msg=r.next();//{done:false,值:“…”}
而(!msg.done){
console.log(msg.value);
msg=r.next();
}
var other=reply.otherFun();
var o=other.next();//{done:false,value:Generator}
而(!o.done){
设gen=o.value;
msg=下一代();
而(!msg.done){
console.log(msg.value);
msg=下一代();
}
o=其他。下一步();
}
var more=reply.evenMore();
msg=more.next();
而(!msg.done){
console.log(msg.value);
msg=more.next();
}
//2019年1月12日更新
//更多例子
for(让r of reply.getReply('for')){
控制台日志(r);
}
for(让r表示答复。evenMore()){
控制台日志(r);
}
//注意