【问题标题】:JavaScript unexpected ReferenceError: Object key value not definedJavaScript 意外 ReferenceError:未定义对象键值
【发布时间】:2018-01-14 18:06:20
【问题描述】:

我正在编写一个 JavaScript Minecraft Blocklauncher 模组。

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) 在 org.mozilla.javascript.ScriptRuntime.constructError(未知来源) 在 org.mozilla.javascript.ScriptRuntime.constructError(未知 来源)在 org.mozilla.javascript.ScriptRuntime.notFoundError(Unknown 来源)在 org.mozilla.javascript.ScriptRuntime.name(未知来源) 在 org.mozilla.javascript.Interpreter.interpretLoop(未知来源) 在 script.Undo(World Editor.js:1369) 在 script.procCmd(World Editor.js:451) 在 org.mozilla.javascript.Interpreter.interpret(未知来源)在 org.mozilla.javascript.InterpretedFunction.call(Unknown Source) at org.mozilla.javascript.ContextFactory.doTopCall(未知来源)在 org.mozilla.javascript.ScriptRuntime.doTopCall(未知来源)在 org.mozilla.javascript.InterpretedFunction.call(Unknown Source) at net.zhuoweizhang.mcpelauncher.ScriptManager.callScriptMethod(ScriptManager.java:288) 在 net.zhuoweizhang.mcpelauncher.ScriptManager.chatCallback(ScriptManager.java:516)

我尝试了许多不同的方法来将数组分配给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)});
  • "x" is not defined. 对我来说似乎很清楚。这个x:undo[i][a].x 没有声明变量x,而是一个属性。
  • @Slai,x 不是在SetBlock() 中定义在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); 中吗?
  • no .. 变量声明将具有等号,如 x = ...

标签: javascript arrays object multidimensional-array


【解决方案1】:

我猜你需要添加这一行:

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.");
 }
}

【讨论】:

  • 没有 x(以及 y、z、块和元数据)。当代码在 SetBlock() 中将 {'x': x, 'y': y, 'z': z, 'block': id, 'metadata': Level.getData(x, y, z)} 推入 undo[undo.length - 1] 时得到声明?
  • @MontyWild 否,它声明了 .x 属性,例如 undo[i][a].x,但没有声明变量 x
【解决方案2】:

我太专注于对象,以至于忘记查看功能...

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.");
 }
}

我需要在 x、y 和 z 前加上 undo[i][a].

【讨论】:

    猜你喜欢
    • 2023-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-26
    • 2021-10-01
    • 2021-10-28
    相关资源
    最近更新 更多