0

A method I am using in Tone.js requires strings as it's arguments. Is there a way to assign the variables up top and then keep them in their quote marks?

This is the notation that works:

var chain = new Tone.CtrlMarkov({
"D2": "D4",
"D4": ["D2","D3"],
"D3":"D2"
});

This is what I want to do (and I've tried all combinations of using quotes, vs not, or using the val1.toString() method

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
});

Thx! The documentation of the library is here and my jsfiddle is here

EJW
  • 604
  • 2
  • 10
  • 22

1 Answers1

3

Here { val1: .., val2: ..., val3: .. }, var1, var2 and var3 are property names literally, the comipler will use them as literals (i.e. not the variable values). So this will not work.

You could use the object[property] notation.

Your code translated:

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);
rnrneverdies
  • 15,243
  • 9
  • 65
  • 95