使用字符串插值进行函数调用
Using string interpolation for function call
有没有办法使用 ES6 语法调用以下函数:
let type = 'Line'
new Chartkick.${ type }Chart(el, this.data)
希望产生:
new Chartkick.LineChart(el, this.data)
不,您不需要为此进行任何字符串插值。只是 standard dynamic property access 加上方括号:
new Chartkick[type+"Chart"](el, this.data);
当然你可以使用 ES6 模板文字而不是字符串连接,但我认为它不会大大提高可读性:
new Chartkick[`${ type }Chart`](el, this.data);
有没有办法使用 ES6 语法调用以下函数:
let type = 'Line'
new Chartkick.${ type }Chart(el, this.data)
希望产生:
new Chartkick.LineChart(el, this.data)
不,您不需要为此进行任何字符串插值。只是 standard dynamic property access 加上方括号:
new Chartkick[type+"Chart"](el, this.data);
当然你可以使用 ES6 模板文字而不是字符串连接,但我认为它不会大大提高可读性:
new Chartkick[`${ type }Chart`](el, this.data);