JavaScript意外的ReferenceError:未定义对象键值

时间:2018-01-14 18:06:20

标签: javascript arrays object multidimensional-array

我正在编写一个JavaScript Minecraft Blocklauncher mod。

var undo = new Array(0);
var redo = new Array(0);

function Checkpoint(FromUI) {
 if (undo === undefined) {
     undo = new Array(0);
 }
 if (undo.length == 0) {
     undo.push(new Array(0));
 }
 else if (undo[undo.length - 1].length > 0) {
  undo.push(new Array(0));
  if (undo.length > size) {
   undo.shift();
  }
 }
 redo = new Array(0);
 if (FromUI !== undefined) {
  Msg("Undo: " + undo.length + " level(s) available.  Redo stack has been cleared.");
 }
}

function SetBlock(x, y, z, id, metadata) {
 if (!Array.isArray(undo[undo.length - 1])) {
  undo[undo.length - 1] = new Array(0);
 }
 undo[undo.length - 1].push({'x': x, 'y': y, 'z': z, 'block': id, 'metadata': Level.getData(x, y, z)});
 setTile(x, y, z, id, metadata);
}

function Undo() {
 if (undo.length > 0) {
  var i = undo.length - 1;
  redo.push(new Array(0));
  for (var a = undo[i].length - 1; a >= 0; a--) {
   redo[redo.length - 1].push({x:undo[i][a].x,
                   y:undo[i][a].y,
                   z:undo[i][a].z,
                   block:getTile(x, y, z),
                   metadata:Level.getData(x, y, z)});
   setTile(undo[i][a].x, undo[i][a].y, undo[i][a].z, undo[i][a].block, undo[i][a].metadata);
  }
  undo.pop();
  Msg("Undo successful.  " + undo.length + " undo level(s) remaining.  " + redo.length + " redo level(s) available.");
 } else {
  Msg("Nothing to undo.  " + redo.length + " redo level(s) available.");
 }
}

Checkpoint()被调用一次,然后多次调用SetBlock()。调用undo()时,会发生错误。

错误行是:

redo[redo.length - 1].push({x:undo[i][a].x, y:undo[i][a].y, z:undo[i][a].z, block:getTile(x, y, z), metadata:Level.getData(x, y, z)});

使用:

  

脚本出现错误:World Editor.js   org.mozilla.javascript.EcmaError:ReferenceError:未定义“x”。   (World Editor.js#1369)at   org.mozilla.javascript.ScriptRuntime.constructError(未知来源)     在org.mozilla.javascript.ScriptRuntime.constructError(未知   来源)org.mozilla.javascript.ScriptRuntime.notFoundError(未知   来源)org.mozilla.javascript.ScriptRuntime.name(未知来源)     在org.mozilla.javascript.Interpreter.interpretLoop(未知来源)     at script.Undo(World Editor.js:1369)at script.procCmd(World   Editor.js:451)at   org.mozilla.javascript.Interpreter.interpret(未知来源)at   org.mozilla.javascript.InterpretedFunction.call(未知来源)at   org.mozilla.javascript.ContextFactory.doTopCall(未知来源)at   org.mozilla.javascript.ScriptRuntime.doTopCall(未知来源)at   org.mozilla.javascript.InterpretedFunction.call(未知来源)at   net.zhuoweizhang.mcpelauncher.ScriptManager.callScriptMethod(ScriptManager.java:288)     在   net.zhuoweizhang.mcpelauncher.ScriptManager.chatCallback(ScriptManager.java:516)

我尝试了许多不同的方法,为undo数组的每个元素分配一个数组,并将具有多个属性的对象分配给undo数组元素中包含的数组,但始终会出现此错误。

为什么会出现此错误,我该如何预防呢?

2 个答案:

答案 0 :(得分:0)

我猜你需要添加这一行:

function Undo() {
 if (undo.length > 0) {
  var i = undo.length - 1;
  redo.push(new Array(0));
  for (var a = undo[i].length - 1; a >= 0; a--) {
   redo[redo.length - 1].push({x:undo[i][a].x,
                   y:undo[i][a].y,
                   z:undo[i][a].z,
                   block:getTile(undo[i][a].x, undo[i][a].y, undo[i][a].z),
                   metadata:Level.getData(undo[i][a].x, undo[i][a].y, undo[i][a].z)});   // <-- changed those 2 lines
   setTile(undo[i][a].x, undo[i][a].y, undo[i][a].z, undo[i][a].block, undo[i][a].metadata);
  }
  undo.pop();
  Msg("Undo successful.  " + undo.length + " undo level(s) remaining.  " + redo.length + " redo level(s) available.");
 } else {
  Msg("Nothing to undo.  " + redo.length + " redo level(s) available.");
 }
}

答案 1 :(得分:0)

我是如此专注于这个对象,我忘了看这些功能......

function Undo() {
 if (undo.length > 0) {
  var i = undo.length - 1;
  redo.push(new Array(0));
  for (var a = undo[i].length - 1; a >= 0; a--) {
   redo[redo.length - 1].push({x:undo[i][a].x,
                   y:undo[i][a].y,
                   z:undo[i][a].z,
                   block:getTile(undo[i][a].x, undo[i][a].y, undo[i][a].z),
                   metadata:Level.getData(undo[i][a].x, undo[i][a].y, undo[i][a].z)});
   setTile(undo[i][a].x, undo[i][a].y, undo[i][a].z, undo[i][a].block, undo[i][a].metadata);
  }
  undo.pop();
  Msg("Undo successful.  " + undo.length + " undo level(s) remaining.  " + redo.length + " redo level(s) available.");
 } else {
  Msg("Nothing to undo.  " + redo.length + " redo level(s) available.");
 }
}

我需要加xy&amp;前缀zundo[i][a].

。{