/    Sign up×
Community /Pin to ProfileBookmark

Java object not populated chronologically

Hi all, I’m building an app using a javascript framework and for days I’ve been scratching my head over some very strange behaviour. After searching for hours I think it boils down to this: when I initiate a new object, use it and populate it afterwards, this doesn’t seem to happen chronologically. When I execute this code, the object I can see in my console already has the field ‘foo’, even though it’s added afterwards. How can this happen?
`
let foo = {}
console.log(foo)
foo.bar = “strange”
`

Thanks in advance!

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 02.2019 — Maybe the console updates the object as your code updates it? Pure speculation on my part, but wondering if that could be the case due to logging an object as opposed to some static string value or such? Maybe see if you get something different by converting it to JSON:
<i>
</i>console.log(JSON.stringify(foo, null, 2))
Copy linkTweet thisAlerts:
@daveyerwinOct 02.2019 — @robbev#1609508

try this ...

<script>

let foo = {}

for(i=0;i<5;++i){

console.log(foo);

foo.bar = 'strange'+i;}

</script>

</script>

logs {}



bar: "strange4"



five times

so in firefox for each iteration of the loop

a reference to the object foo is stored for display in the console

javascript doesn't give up its control over the output to screen

until the loop is finished so

by the time console begins to print the object foo references

the property bar = 4

because console has stored a reference to foo and not a clone of foos state

console displays bar=4 five times
``<i>
</i>&lt;script&gt;
let foo = {};
(function repeat(count){
console.log(foo);
foo.bar = 'strange'+count;
if(count)
setTimeout(function(){var c = count;repeat(--c + 1)},0);
}(4))
&lt;/script&gt;<i>
</i>
``

this logs the count down because

setTimeout breaks the javascript execution and

so allows the console to print the reference before

it is updated
Copy linkTweet thisAlerts:
@robbevauthorOct 05.2019 — @DaveyErwin#1609510

Thank you very much! That does explains the strange behaviour.

In the meantime I took a completely different approach to the problem, but I'm sure this will help me in the future.
×

Success!

Help @robbev spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...