首页 > 解决方案 > Pure vs Impure 方法 ES6 类。我应该传递值还是应该将它们存储在对象中

问题描述

我 100% 确定这个话题之前讨论过,但我没能在谷歌上进行适当的搜索,所以我在这里。

通常,当我编写类时,我有一个特定的方法来做某事,并且在其中我构造每个步骤如下:

  Class MyClass {
    ......


    mainMethod() {
      this.runValidation();
      const val1 = this.method1();
      const val2 = this.method2();
      const val3 = this.method3(val1, val2);
      const val4 = this.methid4(val1, val3);
      ......
      return finalResult
    }
}

我选择在当前对象中只存储一些特定的值。也许这种方式更好,或者我在更多地方使用它并且更容易访问,这取决于。

我的问题是什么是更好的做法。我刚刚在上面向您展示的那个,或者下面一段代码中的那个:

  Class MyClass {
    ......


    mainMethod() {
      this.runValidation();
      this.method1()
      this.method2()
      this.method3()
      ......
      return this.finalResult
    }
}

我在哪里不断更改当前对象的属性?

标签: javanode.jsecmascript-6

解决方案


推荐阅读