繁体   English   中英

是否可以将 function 调用存储在动态变量中?

[英]Is it possible to store a function call in a dynamic variable?

还有一个动态变量名。 我试过这样做:

let positionAx = [450, 420, 425, 425, 430, 444, 449, 453, 449, 471, 475]; // x + z Coordinates for the following units in order: 
let positionAz = [541, 532, 532, 558, 558, 566, 566, 566, 562, 542, 546]; // CC, melee x2, ranged x2, female x4, cavalry x1, special x1

let dynamicShiftx = ["position" + poppedPosition + "x" + ".shift();"]; 
let dynamicShiftz = ["position" + poppedPosition + "z" + ".shift();"]; 
            
let shiftPositionX = dynamicShiftx;
let shiftPositionZ = dynamicShiftz;

dynamicShiftxdynamicShiftz产生我需要的东西,即positionAx.shift(); positionAz.shift(); 但它实际上并没有改变那个价值。 我究竟做错了什么? JS新手顺便说一句。

使用 object 对 arrays 进行分组,然后您可以生成一个字符串键,让您可以使用括号表示法访问所需的数组,如下所示:

let positions = {
  Ax: [450, 420, 425, 425, 430, 444, 449, 453, 449, 471, 475],
  Az: [541, 532, 532, 558, 558, 566, 566, 566, 562, 542, 546],
  Bx: ...
  ...
};

let shiftPositionX = positions[poppedPosition + "x"].shift(); 
let shiftPositionZ = positions[poppedPosition + "z"].shift();

暂无
暂无

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

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