0
var test = {};
test.k1 = {};
console.log(test);
test.k1.k2 = 'apple';
console.log(test);
test.k1.k2 = 'banana';

First log:
Object k1:Object k2:"banana"
WHY? I expect empty object k1... banana is defined 3 lines later

Second log:
Object k1:Object k2:"banana"
WHY? I expect k2: apple... banana is defined in next line

Separd
  • 1
  • 1
  • 2
    This answer might help you. http://stackoverflow.com/questions/23392111/console-log-async-or-sync – Lewis Apr 01 '16 at 07:59
  • @Tresdin: We can go with that as an original for marking this as a dupe. I feel like there's a better version of that out there, but it'll do. *Edit*: Ah, Quentin got there first. – T.J. Crowder Apr 01 '16 at 08:01

1 Answers1

2

Your expectation is correct, it's the console.log that's throwing you. In some environments, particularly browsers, what console.log logs is a live reference to the object, and so if you expand it after the line assigning banana, you see banana instead of apple because it's showing it to you as it was when you expanded it, not when you logged it.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875