【发布时间】:2018-04-29 09:47:35
【问题描述】:
我正在尝试制作一个包含团队积分的对象(如下所示,其中显示“this.update”)。当我运行该程序时,它似乎并没有给团队得分,而且似乎甚至没有评估两个团队的目标。
我希望 team1Points 和 team2Points 属性从 IF 语句(如上述一个)或另一种解决方案中派生出来,类似于两支球队平局 1 分,胜方 3 分,负方 0 分。
teamsArray = ["Blue Team", "Red Team"];
function match(team1Name, team2Name, team1Goals, team2Goals) {
this.team1Name = team1Name;
this.team1Goals = team1Goals;
this.team2Name = team2Name;
this.team2Goals = team2Goals;
this.update = function() {
if (team1Goals > team2Goals) {
team1Points = 3;
team2Points = 0;
} else if (team2Goals > team1Goals) {
team2Points = 3;
team1Points = 0;
} else if (team1Goals == team2Goals) {
team1Points = 1;
team2Points = 1;
}
};
this.update();
}
testMatch();
function testMatch() {
var match1 = new match(teamsArray[0], teamsArray[1], 2, 0);
console.log(match1);
}
【问题讨论】:
-
试试
this.team1Points = 0等
标签: javascript object methods