为什么"建设"不是建构者,而是"资源"是什么?

时间:2018-04-20 14:06:34

标签: javascript jquery

function Resource(name = "undefined", count= 0) {
  this.name = name;
  this.count= count;
}

function Building(name = "undefined", count= 0, cost= 0) {
  this.name = name;
  this.count= count;
  this.cost= cost;
}

function NewBuilding(name) {
  Building[Buildz] = new Building(name);
  createBuildingBtn(name);
  Buildz++;
}

function NewResource(name) {
  Res[Resz] = new Resource(name);
  createResourceDiv(name);
  Resz++;
}

我有这个代码," Building"与资源几乎完全相同。资源工作,但建立dosnt。我希望你能帮助我。

1 个答案:

答案 0 :(得分:2)

您在同一范围内有一个名为Building的函数和一个名为Building的数组。将您的阵列名称更改为其他名称,它可以正常工作Resource有效,因为您为其附带的数组(Res)使用了不同的名称。

Builds = [];
Res = []
Buildz = 0;
Resz = 0;

function Resource(name = "undefined", count= 0) {
  this.name = name;
  this.count= count;
}

function Building(name = "undefined", count= 0, cost= 0) {
  this.name = name;
  this.count= count;
  this.cost= cost;
}

function NewBuilding(name) {
  Builds[Buildz] = new Building(name);
  Buildz++;
}

function NewResource(name) {
  Res[Resz] = new Resource(name);
  Resz++;
}

NewBuilding("Test");
NewResource("Test2");

console.log(Builds);