如何避免重复的JS代码,每个人都使用它?

发布于 2025-01-28 19:42:09 字数 1173 浏览 1 评论 0原文

我有几个不同的类,具有相同的重复代码。除了每个的不同身份外,这些重复的代码表面上都是相同的。

这是一个简化的示例:

class classA {
  ...

  doSomething() {
    if (!this.id || !this.data || !this.isAllowed()) {
      return null;
    }
    return someImportedFunc(this.data, this.metadata, this.doSomethingElse());
  }
}

class classB {
  ...

  doSomething() {
    if (!this.id || !this.data || !this.isAllowed()) {
      return null;
    }
    return someImportedFunc(this.data, this.metadata, this.doSomethingElse());
  }
}

// Many other classes like this

我已经考虑过将dosomething方法移至另一个类,然后在所有这些类中导入它。但这将导致一种非常混乱的方法,该方法需要许多参数来解释所有不同的this的s。看起来像:

class exportedClass {
  function doSomething(id, data, metadata, isAllowed, doSomethingElse) {
    if (!id || !data || !isAllowed()) {
      return null;
    }
    return someImportedFunc(data, metadata, doSomethingElse());
  }
}

class classA {
  ...

  methodThatUsesDoSomething() {
    const result = exportedClass.doSomething(
        this.id, this.data, this.metadata, this.isAllowed, this.doSomethingElse);
    ...
  }
}

除了15个args而不是5个,因为这是一个简化的示例。

在这种情况下,如何避免重复代码?

I have several different classes with the same duplicate code. Aside from the different identity of each's this, these duplicate code are all ostensibly identical.

Here's a simplified example:

class classA {
  ...

  doSomething() {
    if (!this.id || !this.data || !this.isAllowed()) {
      return null;
    }
    return someImportedFunc(this.data, this.metadata, this.doSomethingElse());
  }
}

class classB {
  ...

  doSomething() {
    if (!this.id || !this.data || !this.isAllowed()) {
      return null;
    }
    return someImportedFunc(this.data, this.metadata, this.doSomethingElse());
  }
}

// Many other classes like this

I've thought of moving the doSomething method to another class, then importing it in all these classes. But that would result in a very messy method that takes many, many arguments to account for all the different this's. It'd look like:

class exportedClass {
  function doSomething(id, data, metadata, isAllowed, doSomethingElse) {
    if (!id || !data || !isAllowed()) {
      return null;
    }
    return someImportedFunc(data, metadata, doSomethingElse());
  }
}

class classA {
  ...

  methodThatUsesDoSomething() {
    const result = exportedClass.doSomething(
        this.id, this.data, this.metadata, this.isAllowed, this.doSomethingElse);
    ...
  }
}

Except with like 15 args instead of 5 since this is a simplified example.

How can I avoid duplicate code in this kind of situation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天暗了我发光 2025-02-04 19:42:09

假设您想要一个非传播解决方案,我的建议是将“此”作为参数传递,因为可以将原型操纵为参数。这将使您能够将参数保持在最低限度,同时又不丢失数据。

Assuming you want a non-inheritance solution, my suggestion would be to pass "this" as an argument, as prototypes can be manipulated as parameters. That would allow you to keep the parameters at a minimum while not losing data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文