将函数参数插值为新的变量声明

时间:2018-01-31 21:47:56

标签: javascript node.js

我有一个函数可以创建一个以HTML格式显示的新图表。我想调用带有一些参数的函数插入到图表的变量声明中,如下所示:

function createChart(category, name) {
    const {category}{name}Chart = new Chart();
}

这样做的正确方法是什么?

2 个答案:

答案 0 :(得分:0)

这是一个可能的解决方案:

req.query

答案 1 :(得分:0)

使用new operator会使此constructor调用隐式返回一个新对象。
这意味着您可以使用this关键字来"附加"通过作为参数传递的参数对此新对象的属性。

示例:



function Chart(category, name) {
    this.category = category;
    this.name = name;
}

const chart = new Chart('some category', 'John Doe');
console.log(chart);