loading...

Javascript uses call by sharing for objects

ufko on September 27, 2019

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... [Read Full]
markdown guide
 

You say here that:

It is commonly stated that javascript uses pass by value for objects too and that the value for objects is actually the reference of that object. That's what I thought about javascript's evaluation strategy, too. But that statement is not 100% correct either

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!

 

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

const bar = {};
foo(bar)

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. eg


function foo(bar){
//This will have no effect. Outside foo
bar = {};

}

But if we go to the address and make changes there other people can see it.

function foo(bar){
//This change is visible outside the function.
bar.something = "hello"
}

Now because we always make a photocopy no matter what we pass to a function, if we pass a number

const other = 2;

something(other);

This time it's like we wrote the number 2 on a paper other and then photocopy it and give it to something

If we change the number we are only changing our copy

function something(other){
    //Has no effect outside the function
    other = 2;
}

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

$foo = 2;

bar($foo);

//notice the &
function bar(&$foo){
    $foo = 3;
}

After we call bar in the PHP example $foo will be 3 for EVERYONE not just locally in the function bar.

Hopefully that makes sense.

 

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

 
code of conduct - report abuse