更改类变量的值

时间:2018-12-30 01:11:35

标签: javascript

如果这很简单,我会提前道歉。我实际上需要什么技术术语来搜索文档时,我有些茫然。这是相当简单的东西。我有一个类,该类的实例和要运行的函数,它将更改在类构造函数中建立的值之一。在下面,您将找到一个有效的示例,正​​是我想要的。

./notebook

在函数内部和外部,我尝试了各种加法和减法。我很沮丧此外,我在类似的程序中执行了非常类似的类似步骤,没有任何问题:

class CLASS {
  constructor(name) {
    this._name = name;
    this._round = 0;
    this._wins = 0;
    this._losses = 0;
  }

  get name(){
    return this._name;
  } 

  ...

}

const PL1 = new CLASS('Player 1');

...

function updatePL(player, pointsFor, pointsAgainst) {
  if (pointsFor > pointsAgainst) {
    return player.wins++ } else if ...
  ...

}



console.log(PL1); // Prebiously "console.log(player)" Edited. Not source of issue in actual code.  

updatePL(PL1, 1, 0);

console.log(PL1); //No changes

同样,技术术语使我难以理解,所以我不知道要寻找什么。任何信息,即使只是将我指向Google搜索字词的模糊方向,都是很棒的。谢谢。

1 个答案:

答案 0 :(得分:0)

您可能想用console.log(PL1)代替console.log(player),因为cuz player在updatePL函数的本地。另外,您在课程中使用了_wins,但在wins中使用了updatePL,因此请确保使用相同的语言。

尽管在这之间,为什么要在对PL1进行突变时将其声明为常量? 同样,您也不需要从player.wins返回updatePL,可以像这样PL1.wins.PL1.your_getter

一样从对象本身访问此prop
相关问题