JS - 对象构造函数方法

3
我正在尝试制作一个对象,其中包括团队得分(如下所示,“this.update”处)。 当我运行程序时,它似乎没有将积分给予团队,并且甚至没有评估双方的目标。
我希望从类似于上面的IF语句或其他解决方案中派生team1Points和team2Points属性,类似于平局每个团队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);
}


1
尝试使用 this.team1Points = 0 等语句。 - Mark
3个回答

1

你的方法创建了全局变量而不是属性,而且你甚至没有调用它!

为了避免这种问题,我建议切换到现代JavaScript语法。在脚本开头使用指令'use strict';启用严格模式,并使用let定义变量,而不是var。如果你这样做,浏览器将不会让你从函数内部定义全局变量。

至于你代码的解决方案:

function match(team1Name, team2Name, team1Goals, team2Goals) {
  this.team1Name = team1Name;
  this.team1Goals = team1Goals;
  this.team2Name = team2Name;
  this.team2Goals = team2Goals;
  this.team1Points = this.team2Points = 0;

  this.update = function() {
    if (this.team1Goals > this.team2Goals) {
      this.team1Points = 3;
      this.team2Points = 0;
    } else if (this.team2Goals > this.team1Goals) {
      this.team2Points = 3;
      this.team1Points = 0;
    } else if (this.team1Goals == this.team2Goals) {
      this.team1Points = 1;
      this.team2Points = 1;
    }
  };
}

不要忘记在某个地方调用.update()
m = new match("Alpha", "Beta", 0, 2);
m.update();

console.log("Team " + m.team1Name + " has " + m.team1Points + " points.");
console.log("Team " + m.team2Name + " has " + m.team2Points + " points.");

1
哇,如果你注意到了那个可怕的编辑序列,我很抱歉。我在使用SO方面有点生疏了。 - Domino
1
更新函数不返回任何内容,因此记录其结果是没有作用的。如果要检查变量的值,您需要记录变量本身。 - Domino
我该怎么做?我对此感到相当困惑。 - Jay
1
@Jay 我已经编辑了答案,并且给出了一个示例,说明如何创建一个匹配并记录结果。 - Domino
1
啊,我懂了。我忘记在条件语句中加上 this. 了。 - Domino
显示剩余2条评论

1
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;
      return "team1 win"
    } else if (team2Goals > team1Goals) {
      team2Points = 3;
      team1Points = 0;
      return "team2 win"
    } else if (team1Goals == team2Goals) {
      team1Points = 1;
      team2Points = 1;
      return "draw"
    }
  };
}

testMatch();

function testMatch() {
  var match1 = new match(teamsArray[0], teamsArray[1], 2, 0);

  console.log(match1.update());
}

1

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) {
      this.team1Points = 3;
      this.team2Points = 0;
    } else if (team2Goals > team1Goals) {
      this.team2Points = 3;
      this.team1Points = 0;
    } else if (team1Goals == team2Goals) {
      this.team1Points = 1;
      this.team2Points = 1;
    }
  };
  this.update();
}

testMatch();

function testMatch() {
  var match1 = new match(teamsArray[0], teamsArray[1], 2, 0);
  console.log(match1);
}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接