I recently watched a course about javascript on Udemy. It was a great course from a great instructor who provided great content - except for one th...
For further actions, you may consider blocking this person and/or reporting abuse
You say here that:
Isn't that what call by sharing is? I found this SO answer that says the same thing
Yes, that is what call by sharing is 👍 When I google "is javascript pass by value" I see results which says "javascript is pass by value". Only a few of them mentions the term call by sharing. So I'm just interested in the terminology here.
Yeah I had forgotten what call by sharing was until I read your post. I'd gotten used to calling it "call by reference"! I think Python uses the same model if I'm not wrong. Anyway, welcome to DEV!
Thanks! 😄
I tried to search any academic source on "call-by-sharing". The only thing I found so far is the manual for theta.
Do you know example of a language which has mutations and uses call-by-value?
I had trouble finding academic resources on "call by sharing", too 🤷♂ C uses call by value and has mutations
I'm still kinda confused.
So if JavaScript uses pass by sharing, but pass by sharing resembles pass by reference, whats the difference?
You mention there is "copy" mechanism when passing the object to the function, which I guess becomes the new "reference" of the object?
Do you have an example where JavaScript would work differently if it uses pass by reference? Or is this all how the JS engine actually handles stuff under the hood?
Okay so when JS passes and argument to a function you get something like this
bar
we can think of as a piece of paper saying where to find the data.Then when we give bar to the function
foo
JavaScript photocopies that piece of paper and gives it to us. If we alter the paper it won't change anything about what's at bar's location. egBut if we go to the address and make changes there other people can see it.
Now because we always make a photocopy no matter what we pass to a function, if we pass a number
This time it's like we wrote the number 2 on a paper
other
and then photocopy it and give it tosomething
If we change the number we are only changing our copy
So if a language is pass by reference what are we doing? We are handing the function that same piece of paper and NOT making a copy of it. And when we are done we give it back to whoever called us. So any changes we make to the paper gets reflected outside the function.
PHP has this functionality
After we call
bar
in the PHP example$foo
will be3
for EVERYONE not just locally in the functionbar
.Hopefully that makes sense.
I think this post is mildly confusing and misleading.
For something super in depth
JavaScript, Ruby and C are not call by reference
Derk-Jan Karrenbeld ・ Jun 27 ・ 9 min read