简体   繁体   English

JS中传递生成器函数的最佳实践

[英]Best practice for passing through generator function in JS

I am trying to figure out the best practice of passing a generator (including yield) function through another function as is.我试图找出将生成器(包括 yield)函数按原样传递给另一个函数的最佳实践。

In other words, I am looking for a better way to achieve the following.换句话说,我正在寻找一种更好的方法来实现以下目标。

Let's say that I have class ServiceA and ServiceB , that own the same method generate , that generates some sequences:假设我有类ServiceAServiceB ,它们拥有相同的方法generate ,生成一些序列:

class ServiceA extends ServiceBase {
    // ...

    * generate() {
        const length = Math.round(Math.rand() * 100);
        for (let i=0; i<length; i++)
            yield i;
        return -1;
    }

    // ...
}

class ServiceB extends ServiceBase {
    // ...

    * generate() {
        const length = Math.round(Math.rand() * 100) + 100;
        for (let i=100; i<length; i++)
            yield i;
        return 13;
    }

    // ...
}

Now, I have a wrapper class ServiceName that uses class ServiceA or ServiceB , but also, passing through the generate method from these classes:现在,我有一个包装类ServiceName ,它使用类ServiceAServiceB ,而且还通过这些类的generate方法:

class ServiceManager {
    #service;

    constructor(serviceName) {
        // The following require will return ServiceA or ServiceB instance
        this.#service = require('./service/' + serviceName);
    }

    * generate() {
        for ( let i of this.#service.generate() )
            yield i;
    }

    // ...
}

And extension is not an option, as this is a design pattern I need.扩展不是一种选择,因为这是我需要的设计模式。

Two questions regarding the generate method within class ServiceManager :关于ServiceManager类中的generate方法的两个问题:

  1. How do I passthrough a return in addition to yield ?除了yield我如何传递return
  2. Is there a better and more clean way to implement it?有没有更好,更干净的方法来实现它?
  1. How do I passthrough a return in addition to yield ?除了yield我如何传递return
* generate() {
    return yield* this.service.generate();
}
  1. Is there a better and more clean way to implement it?有没有更好,更干净的方法来实现它?
generate() {
    return this.service.generate();
}
 #service;
this.#service = …
return this.#service.generate();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将参数传递给事件处理程序函数的最佳实践 - Best practice for passing arguments to an event handler function 通过 JS 高效地设置元素样式 - 最佳实践 - Style elements through JS efficiently - best practice JS在对象内部传递带有绑定的参数以访问对象范围内特定变量的最佳实践 - JS Best practice for passing argument with bind to function inside object to access specific variable inside objects scope 在不执行参数的情况下传递带有参数的Javascript函数的最佳实践是什么? - What's the best practice for passing a Javascript function with parameters without executing it? js超时最佳实践 - js timeout best practice Yeoman 生成器运行异步最佳实践 - Yeoman generator run async best practice 快递JS通过controller function传递中间件 - Express JS passing middleware through controller function 用于异步功能的js生成器 - js generator for asynchronous function 将枚举列表传递给外部 JS 文件的最佳实践? ASP.NET 核心MVC - Best practice for passing an enum list to an external JS file? ASP.NET Core MVC 将数据从一个 Vue.js 组件传递到 Laravel 中的另一个组件的最佳实践 - best practice for passing data from one Vue.js component to another in Laravel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM