- 1

['foo' + ++i]: i ◯Is "++i" calculated later? (than "i" , after colon, is set)

The following code is from JS Tutorial>ES6>ES6 Objects>Computed Property Names>ex2 -- var i = 0; var a = { ['foo' + ++i]: i, ['foo' + ++i]: i, ['foo' + ++i]: i }; console.log(a.foo1); // 1 console.log(a.foo2); // 2 console.log(a.foo3); // 3 -- (※the output is 0 1 2) ◯Why the output is different from the expected one (in its comment)? ◯Is "++i" always calculated after the value "i" (the one located after colon) is set? ◯Otherwise, is there another reason? https://code.sololearn.com/WYujXs5Yk995/?ref=app

19th Apr 2020, 11:49 PM
kiclause
kiclause - avatar
3 Réponses
+ 2
I tested the code in Code Playground and it outputs exactly as the comment e.g. 1, 2 and 3 in console. Not sure why you got 0, 1 and 2 instead. Let's see what others has to say later.
20th Apr 2020, 4:12 AM
Ipang