/    Sign up×
Community /Pin to ProfileBookmark

Javascript Threads – Confusing

If I run the following code:

let data = []

person = {}
person[“name”] = “Bob”
person[“age”] = 30
person[“skills”] = {}
person[“skills”][“programming”] = “.NET”
data.push(person)

person = {}
person[“name”] = “Frank”
person[“age”] = 50
person[“skills”] = {}
person[“skills”][“programming”] = “Javascript”
data.push(person)

console.log(data)
I get the expected output:

[
{ name: ‘Bob’, age: 30, skills: { programming: ‘.NET’ } },
{ name: ‘Frank’, age: 50, skills: { programming: ‘Javascript’ } }
]
But if I run the same code but comment out resetting the person variable:

let data = []

person = {}
person[“name”] = “Bob”
person[“age”] = 30
person[“skills”] = {}
person[“skills”][“programming”] = “.NET”
data.push(person)

// person = {}
person[“name”] = “Frank”
person[“age”] = 50
person[“skills”] = {}
person[“skills”][“programming”] = “Javascript”
data.push(person)

console.log(data)
I get this …

[
{ name: ‘Frank’, age: 50, skills: { programming: ‘Javascript’ } },
{ name: ‘Frank’, age: 50, skills: { programming: ‘Javascript’ } }
]
Javascript concurrency, threading, synchronicity, it is all a mystery to me. How do I tell what is synchronous and what is asynchronous?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@daveyerwinDec 05.2021 — Javascript concurrency, threading and synchronicity

are not responsible for the results of the code

you posted.

try this script ...
``<i>
</i>&lt;script&gt;

&gt; person = {}
&gt; person["name"] = "Bob"
&gt; person["name"] = "Frank"
&gt; alert(person["name"])//alerts Frank
&gt; copyOfPerson = person;
&gt; copyOfPerson['name']='John'
&gt; alert(person["name"])//alerts John
&gt; &lt;/script&gt;<i>

</i>
``

when copyOfPerson's name property was changed

person's name property was also changed!

WHY?

because both copyOfPerson and person both REFER to the SAME object

https://codeburst.io/explaining-value-vs-reference-in-javascript-647a975e12a0
Copy linkTweet thisAlerts:
@daveyerwinDec 05.2021 — > @nsteblay#1640181 I get this ...
>
> [

> { name: 'Frank', age: 50, skills: { programming: 'Javascript' } },

> { name: 'Frank', age: 50, skills: { programming: 'Javascript' } }

> ]

yes

you pushed a reference to an object twice
``<i>
</i>&lt;script&gt;
let data = []
person = {}
data.push(person)
data.push(person)
console.log(data)
person["name"] = "Bob"
person["age"] = 30
person["skills"] = {}
person["skills"]["programming"] = ".NET"
&lt;/script&gt;<i>
</i>
``

in chrome and edge

console.log(data)

is not executed until the execution of the block of code

console.log(data)

is contained in is through executing so when

console.log(data)

is finally executed the properties of the two references to person are already set
×

Success!

Help @nsteblay 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.25,
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,
)...