0

How can I achieve that:

function Item(name, company) {
    this.name = name;
    [company] = 'Google';
}
var company = 'internet';
var test = new Item('someone', company);

test.name return 'someone'.

test.internet should return 'Google'.

Thank you in advance !

Philippe
  • 960
  • 2
  • 12
  • 29
  • 1
    @Quentin: I'm looking for an example of using that in a constructor. Can't find a better dupe... – Cerbrus Sep 28 '17 at 13:58

1 Answers1

1

Simple. Just like name, you need to assign the variable key to this.

function Item(name, company) {
    this.name = name;
    this[company] = 'Google';
}
var company = 'internet';
var test = new Item('someone', company);

console.log(test.internet)
Cerbrus
  • 70,800
  • 18
  • 132
  • 147