我在Tone.js中使用的方法需要字符串作为其参数。有没有办法将变量分配到顶部,然后将它们保留在引号中?
这是有效的符号:
var chain = new Tone.CtrlMarkov({
"D2": "D4",
"D4": ["D2","D3"],
"D3":"D2"
});
这就是我想要做的事情(我已尝试过使用引号的所有组合,相比之下,或使用val1.toString()方法
var val1 = "D2"; //trying using quotes
var val2 = D4; //trying not using quotes
var val3 = D3;
var chain = new Tone.CtrlMarkov({
val1: val2,
val2: [val1,val3],
val3.toString(): val1 //trying toString method
});
答案 0 :(得分:3)
这里{ val1: .., val2: ..., val3: .. }
,var1,var2和var3是属性名字面意思,comipler将它们用作文字(即不是变量值)。所以这不会奏效。
您可以使用object[property]
表示法。
您的代码已翻译:
var var1 = "D1";
var var2 = "D2";
var var3 = "D3";
// create a empty object
var obj = {};
// fill the object with the data using the object[property]
obj[var1] = var2; // this is equal to obj.D1 = "D2"
obj[var2] = [ var2, var3 ]; // this is equal to obj.D2 = [ "D1", "D3" ]
obj[val3] = var1; // this is equal to obj.D3 = "D1"
// then use the object.
ar chain = new Tone.CtrlMarkov(obj);