我如何实现这一目标:
function Item(name, company) {
this.name = name;
[company] = 'Google';
}
var company = 'internet';
var test = new Item('someone', company);
test.name
返回'someone'
。
test.internet
应该返回'Google'
。
提前谢谢!
答案 0 :(得分:1)
简单。与name
一样,您需要将变量键分配给this
。
function Item(name, company) {
this.name = name;
this[company] = 'Google';
}
var company = 'internet';
var test = new Item('someone', company);
console.log(test.internet)