【发布时间】:2021-09-27 11:52:07
【问题描述】:
我是 JS 新手,所以我尝试使用关联数组,但在运行以下 JS 代码时出现错误。
未捕获的类型错误:无法设置未定义的属性(设置“x”)
任何协助都会很棒。
var activeField;
fields = []; // holds all the defined fields
function Field() {
this.polypoints = [{}];
}
var field = new Field();
var Points;
fields.push(field);
activeField = (fields.length) - 1;
Points = fields[activeField].polypoints.length;
Points = Points + 1;
fields[activeField].polypoints[Points].x = 1;
fields[activeField].polypoints[Points].y = 1;
console.log("values in array:", activeField, polyPoints, fields);
【问题讨论】:
-
关联数组仅存在于 PHP 中。它们在 JavaScript 中的等价物是对象。
-
应该声明字段 var
标签: javascript associative-array