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。我希望你能帮助我。
答案 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);