[英]Changing values of a Class variable
I'll apologise in advance, if this is something straightforward. 如果这很简单,我会提前道歉。 I'm at a bit of a loss as to what technical terms I need to actually search for the documentation on this.
我实际上需要什么技术术语来搜索文档时,我有些茫然。 It's fairly straight forward stuff.
这是相当简单的东西。 I have a class, an instance of that class and a function I want to run, that would change one of the values established in the class constructor.
我有一个类,该类的实例和要运行的函数,它将更改在类构造函数中建立的值之一。 Below you'll find an example that's effectively, exactly what I'm aiming for.
在下面,您将找到一个有效的示例,正是我想要的。
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
I've tried various means of adding and subtracting, both inside and outside the function. 我尝试了函数内部和外部的各种加法和减法。 I'm stumped.
我很沮丧 Moreover, I was performing fairly similar similar steps inside something like this with no issues:
而且,我在类似的东西中执行了非常类似的类似步骤,没有任何问题:
object = [{
a: 1,
b: 2,
},
{
...
}];
Again, the technical terms elude me so I don't known what to look for. 同样,技术术语使我难以理解,所以我不知道要寻找什么。 Any information, even if it's just pointing me in the vague direction of a google search term, would be fabulous.
任何信息,即使只是将我指向Google搜索字词的模糊方向,都是很棒的。 Thank you.
谢谢。
You might want to console.log(PL1)
instead of console.log(player)
, cuz player is local to your updatePL
function. 您可能想用
console.log(PL1)
而不是console.log(player)
,因为cuz player在您的updatePL
函数中是本地的。 Also you used _wins
in the class but wins
in updatePL
so make sure you're using the same. 另外你用
_wins
该类中,但是wins
在updatePL
所以一定要确保你使用的是相同的。
In between though, why declare PL1 as constant when you're mutating it ? 但是,在两者之间,为什么要在对其进行突变时将其声明为常量? Also you don't need to return
player.wins
from updatePL
, you can alway access this prop from the object itself like so PL1.wins.
另外,您不需要从
updatePL
返回player.wins
,就可以像PL1.wins.
一样从对象本身访问此prop PL1.wins.
or PL1.your_getter
或
PL1.your_getter
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.