Gunnar Bittersmann: Referenz im Objektliteral

Beitrag lesen

Ich hätte gern sowas wie

let myObject = {
	array1: [1, 2, 3],
	array2: [2, 3, 4]
};

Nur dass sich array2 aus array1 berechnen soll:

let myObject = {
	array1: [1, 2, 3],
};

myObject.array2 = myObject.array1.map(x => x + 1);

Kriegt man das irgendwie in das Objektliteral mit rein?

let myObject = {
	array1: [1, 2, 3],
	array2: myObject.array1.map(x => x + 1)
};

ergibt: ReferenceError: can't access lexical declaration `myObject' before initialization

let myObject = {
	array1: [1, 2, 3],
	array2: array1.map(x => x + 1)
};

ergibt: ReferenceError: array1 is not defined

let myObject = {
	array1: [1, 2, 3],
	array2: this.array1.map(x => x + 1)
};

ergibt: TypeError: this.array1 is undefined

LLAP 🖖

--
“When UX doesn’t consider all users, shouldn’t it be known as ‘Some User Experience’ or... SUX? #a11y” —Billy Gregory