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)
1
  • 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 Commented Feb 22, 2018 at 20:16

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.