我将此数组声明为:
var gamme =
[
["A"],
["A♯","B♭"],
["B","C♭"],
["C","B♯"],
["C♯","D♭"],
["D"],
["D♯","E♭"],
["E","F♭"],
["F","E♯"],
["F♯","G♭"],
["G"],
["G♯","A♭"],
];
使用这个数组,我正在生成另一个数组。 我想要气质埃加尔看起来像这样: 例如:
temperamentEgal[10] = [103.82617439498631, ["G♯","A♭"]]
temperamentEgal[25] = [233.08188075904502, ["A♯","B♭"]]
所以这就是我所做的:
base = pow(2,1/12); // 2^(1/12)
for (i=0; i<12*octaves; i++) // octaves = 6
{
temperamentEgal[i] = []
temperamentEgal[i][0] = 55*pow(base,i); // = 138.6
temperamentEgal[i][1] = gamme[i%12]; // = ["C♯","D♭"]
console.log("gamme["+i%12+"] = " + gamme[i%12]);
for (j=0; j<temperamentEgal[i][1].length; j++)
{
var octaveNote = 1+i/12;
octaveNote = floor(octaveNote);
temperamentEgal[i][1][j] += str(octaveNote);
}
}
正如您所看到的,我从未改变gamme
的值
尽管如此,随着循环的进行,我从console.log获得了这个:
gamme[0] = A
gamme[1] = A♯,B♭
gamme[2] = B,C♭
gamme[3] = C,B♯
gamme[4] = C♯,D♭
gamme[5] = D
gamme[6] = D♯,E♭
gamme[7] = E,F♭
gamme[8] = F,E♯
gamme[9] = F♯,G♭
gamme[10] = G
gamme[11] = G♯,A♭
gamme[0] = A1
gamme[1] = A♯1,B♭1
gamme[2] = B1,C♭1
gamme[3] = C1,B♯1
gamme[4] = C♯1,D♭1
gamme[5] = D1
gamme[6] = D♯1,E♭1
gamme[7] = E1,F♭1
gamme[8] = F1,E♯1
gamme[9] = F♯1,G♭1
gamme[10] = G1
gamme[11] = G♯1,A♭1
如果我发表评论temperamentEgal[i][1][j] += str(octaveNote);
,那就不再发生了。
为什么更改temperamentEgal[i][1]
的值也会更改gamme[i%12]
的值?
答案 0 :(得分:2)
当您不更改gamme
时,您更改此行引用的匿名数组:
temperamentEgal[i][1] = gamme[i%12];
此赋值的右侧和左侧现在指向相同的Array对象 - 如果要修改Array的副本,则应复制Array的值,而不是对Array对象的引用:
ES5:temperamentEgal[i][1] = gamme[i%12].slice();
ES6:temperamentEgal[i][1] = [...gamme[i%12]]
使用spread operator