Class ES 6 JS 的继承问题及其失败的测试

Class Inheritance issue for ES 6 JS and Its failing tests

我是一名来自 Ruby 背景的新开发者。最近我一直在 JS 上非常努力地工作,我在新的 ES 6 中遇到了 class 继承的一些问题。我觉得这可能是我对 JS 的理解的问题,或者将它与 Ruby。我一直在尝试将一个 Ruby 项目翻译成 JS 来练习,但我现在没有通过功能测试。

Failing Feature test when trying to initialize two instances of a class

const STATS = { str:1, dex:1, int:1 }

class Example {
  constructor(race, clas) {
    this.race = race,
    this.clas = clas,
    this.stats = this.add(STATS)
  }
  
  add(stats) {
    if(this.race != 'empty'){
      stats.str += this.race.str
      stats.dex += this.race.dex
      stats.int += this.race.int
    }
    if(this.clas != 'empty') {
      stats.str += this.clas.str
      stats.dex += this.clas.dex
      stats.int += this.clas.int
    } 
    return stats
  }
}

var a = new Example({str: 1, dex:0, int:0}, 'empty');
var b = new Example('empty', {str: 0, dex:0, int:1});

console.log('Should be str:2 dex:1 int:1');
console.log(a.stats); 
console.log('Should be str:1 dex:1 int:2');
console.log(b.stats);

我的 class 具有在构造时更改状态的函数,但问题是任何时候调用新的 Class 它都会保留前一个变量的更改。这只是我的功能测试中的一个问题,因为这是唯一一次 class 被调用两次。

这是 link 我的功能测试 https://github.com/RyanWolfen7/jsRPG/blob/master/cypress/integration/featureTest/characterFeature_test.js

这是未通过测试的 class https://github.com/RyanWolfen7/jsRPG/blob/master/models/characters/character.js

老实说,我可能会放弃我的项目并重新开始,但我想了解我的问题所在。我正在对 JS 采用 OOD 方法并将我的 ruby 项目 https://github.com/RyanWolfen7/ruby_rpg 转换为 JS。我不确定是因为我写错了测试还是对 es-6 的工作方式有深刻的误解。

我尝试过的事情:

创建新对象

正在将新创建的对象分配给 new class

这不是继承问题。事实上,它与 OO 完全无关。您所看到的是 javascript 中的大多数内容都是引用(指针)这一事实的结果,但您正在编写代码,就好像 STATS 是一个值。

在你的函数中 add 你这样做:

add(stats) {
  if(this.race != 'empty'){
    // modifying passed in object, not creating new object
    stats.str += this.race.str
    stats.dex += this.race.dex
    stats.int += this.race.int
  }
  if(this.clas != 'empty') {
    // modifying passed in object, not creating new object
    stats.str += this.clas.str
    stats.dex += this.clas.dex
    stats.int += this.clas.int
  } 
  return stats
}

因此,无论您调用 add() 多少次,无论您从哪个 Example 实例调用它,您都只是访问和覆盖单个共享 STATS 对象。

要在每次函数调用时创建 STATS 的新副本,您需要将其复制到新对象。最快的老式方法是将对象序列化为字符串,然后将字符串转换回对象:

add (input) {
  var stats = JSON.parse(JSON.stringify(input));

  // ...
}

这感觉很难看,但多个基准测试确实表明它是最快的方法。

现代 javascript 可以使用 Object.assign:

add (input) {
  var stats = Object.assign({},input);

  // ...
}

不过不知道是不是快点。您必须自己对其进行基准测试。您可以 google 短语 "js clone object" 了解更多信息。