繁体   English   中英

JavaScript,为什么会出现错误“函数add中未定义数组”

[英]JavaScript, why am I getting the error “array is not defined in the function add”

这是我的代码

var chars = new function() {
  this.array = new Array(0);
  this.add = function(x, y, speed, size, color, ai) {
    this.array[array.length] = {x: x, y: y, size: size, color: color, ai: ai};
  }
}

在第35行,该函数添加的唯一一行我得到了错误“未定义数组”,为什么会这样,是的,我确实尝试过,

chars.array[array.length] = {x: x, y: y, size: size, color: color, ai: ai};

您必须将其修复为:

var chars = new function() {
    this.array = new Array(0);
    this.add = function(x, y, speed, size, color, ai) {
        this.array[this.array.length] = {x: x, y: y, size: size, color: color, ai: ai};
    }
}

我已经将this.array[array.length]更改为this.array[this.array.length]

我建议您使用push方法。 push方法将值附加到数组。

 this.array.push({x: x, y: y, size: size, color: color, ai: ai});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM