JS - 对象构造方法

时间:2017-11-16 00:46:36

标签: javascript object methods

我正在尝试创建一个包含团队积分的对象(如下所示,其中" this.update")。当我运行该程序时,它似乎没有给团队提供积分,而且似乎甚至没有评估两个团队和#39;目标

我希望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);
}




3 个答案:

答案 0 :(得分: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 :(得分: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);
}




答案 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());
}