簡體   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