0

Is there a way to call the following function using ES6 syntax:

let type = 'Line'
new Chartkick.${ type }Chart(el, this.data)

with the hopes of generating:

new Chartkick.LineChart(el, this.data)
Jeremy Thomas
  • 6,240
  • 9
  • 47
  • 92
  • I don't think so, however you can use this syntax: ```let type = 'LineChart';``` and then ```new Chartkick[type](el, this.data);``` this is not an es6 specific syntax however – Varinder Feb 22 '18 at 20:16

1 Answers1

3

No, you don't need any string interpolation for this. It's just standard dynamic property access with bracket notation:

new Chartkick[type+"Chart"](el, this.data);

Of course you could use an ES6 template literal instead of the string concatenation, but I don't think it boosts readability a lot:

new Chartkick[`${ type }Chart`](el, this.data);
Bergi
  • 630,263
  • 148
  • 957
  • 1,375