简体   繁体   English

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

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

Also with a dynamic variable name.还有一个动态变量名。 I tried doing it so:我试过这样做:

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;

both dynamicShiftx and dynamicShiftz yield what i need which is positionAx.shift(); dynamicShiftxdynamicShiftz产生我需要的东西,即positionAx.shift(); and positionAz.shift();positionAz.shift(); but it doesnt actually shift that value.但它实际上并没有改变那个价值。 What am i doing wrong?我究竟做错了什么? js newbie btw. JS新手顺便说一句。

Use an object to group the arrays, then you can generate a string key that lets you access the array you want using bracket notation like so:使用 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.

相关问题 设置一些变量后,是否可以存储并执行javascript函数调用? - Is it possible to store a javascript function call and execute it when some variable is set? 将函数的“调用”函数存储在变量中 - Store the “call”-function of a function in a variable 是否可以在变量上调用jquery函数? - Is it possible to call a jquery function on a variable? 为什么可以在变量中存储原型函数? - Why is it possible to store a function of the prototype inside a variable? 是否可以使用外部变量从另一个函数调用一个函数? - Is it possible to call a function from another function, using an external variable? 是否可以存储在php变量中返回字符串的JavaScript函数? - Is it possible to store a JavaScript function that returns a string in a php variable? 可以将dojo验证器函数存储到变量中,以便以后调用它 - It is possible to store a dojo validator function into a variable to invoke it later 如何创建回调函数并存储变量的值 - How to create a call back function and store the value of the variable 我想在变量中存储一个 function 并想在 onclick 事件中调用它 - I want to store a function in variable and want to call that at onclick event Function 存储 fetch api 调用作为局部变量(不是承诺) - Function to store fetch api call as local variable (not promise)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM