2

I try to use an object value inside the object creation. But the log says the the value is undefined.

btn.wert4 should have the same value as btn.wert1. Is "this" the wrong way?

var btn = [];

btn.push({
  wert1: 'aaa',
  wert2: 'bbb',
  wert3: 'ccc',
  wert4: this.wert1
})

console.log(btn);

Thanks for your help!

InFlames82
  • 359
  • 2
  • 12

1 Answers1

0

The get syntax binds an object property to a function that will be called when that property is looked up.

var btn = [];

btn.push({
  wert1: 'aaa',
  wert2: 'bbb',
  wert3: 'ccc',
  get wert4() {
    return this.wert1
  }
})

console.log(btn);
Rayon
  • 34,175
  • 4
  • 40
  • 65