【问题标题】:JS - Object Constructor MethodsJS - 对象构造方法
【发布时间】: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


【解决方案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.");

【讨论】:

  • 哇,抱歉,如果您注意到那可怕的编辑序列。我对 SO 的使用有点生疏了。
  • 我忘了添加 team1Points 和 team2Points 变量,但它并没有真正回答我的问题。我试图做 console.log(testMatch.update()) 并没有真正解决问题。我正试图让 team1Points = 3,因为他们进了更多的球。也许我不确定调用 update() 函数是错误的
  • 更新函数不返回任何内容,因此不会记录其结果。如果要检查变量的值,则需要自己记录变量。
  • @Jay 我编辑了答案,并举例说明了如何创建匹配并记录结果。
  • 啊,明白了。我也忘了在条件中添加this.
【解决方案2】:

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);
}

【讨论】:

    【解决方案3】:
    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());
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-23
      • 1970-01-01
      • 2018-03-03
      • 2023-03-13
      • 1970-01-01
      • 2016-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多