javascript 返回奇怪结果的逻辑

javascript logic returning strange results

我有一个网格,我正在尝试将光标编程到该网格上,网格中有一个中心点,我试图让光标只能从该中心点移动 x 个方格,这里是我的代码

var makecursor = function (axis, operation) {
    oppositeAxis = axis === 'y' ? 'x' : 'y';

    // app.cursor contains the x and y coordinates of the cursor
    app.cursor[axis] = 

    // calcualte the difference between the current cursor location and the origin
    Math.abs( app.cursor[axis] - origin[axis] ) + 

   // calculate the difference between the opposite axis and
   // the origin and add it to the previous calculation
    Math.abs( app.cursor[oppositAxis] - origin[oppositeAxis] ) 

  // add the operation to be performed ( movement of cursor, 1 or -1 )
  + operation 

  // if the sum of the x, y and current operation are greater then the allowed
  // movement, then make "app.cursor[axis]" equal to itself (dont move) , 
  // otherwise make "app.cursor[axis]" equal to itself plus the operation ( move )

> origin.movement ? app.cursor[axis] : app.cursor[axis] + operation;
}

"operation" 为 1 或 -1,方向

"origin.movement" 是从原点开始可以移动光标的方格数。

我的希望/预期行为是,从我的网格上的一个正方形,您只能移动 "origin.movement" 变量中指定的正方形数量。但是当我打印出结果时它返回了奇怪的数字,并且它没有正确计算位置,即原点应该是零,而是一个或两个,这取决于之前的动作,还有许多我没有的其他异常能够理解。任何解决此问题的帮助将不胜感激,谢谢!

您需要在要测试的表达式周围加上括号 > origin.movement 以便在那里使用该表达式的结果;没有它们,表达式会提前分解:

var makecursor = function (axis, operation) {
    var oppositeAxis = axis === 'y' ? 'x' : 'y';

    app.cursor[axis] = (
        // calculate the difference between the current cursor location and the origin
        Math.abs( app.cursor[axis] - origin[axis] ) + 

        // calculate the difference between the opposite axis and
        // the origin and add it to the previous calculation
        Math.abs( app.cursor[oppositeAxis] - origin[oppositeAxis] ) +

        // add the operation to be performed ( movement of cursor, 1 or -1 )
        operation

        // if the sum of the x, y and current operation are greater then the allowed
        // movement, then make "app.cursor[axis]" equal to itself (dont move) , 
        // otherwise make "app.cursor[axis]" equal to itself plus the operation ( move )
    ) > operation ? app.cursor[axis] : app.cursor[axis] + operation;
}

但是,无论如何,我不会在一条长长的线上做一个自我赋值,为了清晰和便于调试,我会把它拆开,并且只需使用 if:

所以:

var makecursor = function (axis, operation) {
    var oppositeAxis = axis === 'y' ? 'x' : 'y';

    var movement = 
        // calculate the difference between the current cursor location and the origin
        Math.abs( app.cursor[axis] - origin[axis] ) + 

        // calculate the difference between the opposite axis and
        // the origin and add it to the previous calculation
        Math.abs( app.cursor[oppositeAxis] - origin[oppositeAxis] ) +

        // add the operation to be performed ( movement of cursor, 1 or -1 )
        operation;

    // If the sum of the x, y and current operation are less than or equal
    // to the allowed movement, add the operation to `app.cursor[axis]`
    if (movement <= origin.movement) {
        app.cursor[axis] += operation;
    }
}

旁注:您的代码也因未声明 [​​=14=] 变量而成为 The Horror of Implicit Globals 的牺牲品(稍后使用它时也有错字)。我已经修复了拼写错误并通过在上面添加 var 来修复隐式全局。

我明白了,我没有正确考虑操作,我需要将操作添加到我正在移动的轴上,所以声明

    Math.abs( app.cursor[axis] - origin[axis] ) + 

    // calculate the difference between the opposite axis and
    // the origin and add it to the previous calculation

    Math.abs( app.cursor[oppositeAxis] - origin[oppositeAxis] ) +

    // add the operation to be performed ( movement of cursor, 1 or -1 )
    operation;

其实应该是这样的:

// add operation to curser location to account for movement
Math.abs(( app.curser[axis] + operation ) - origin[axis] ) + 

// instead of the adding operation at the end
Math.abs( (app.curser[oppositeAxis]) - origin[oppositeAxis] );