Giter Site home page Giter Site logo

Comments (8)

davegregg avatar davegregg commented on May 2, 2024 1

The correct term is "call by sharing" or "pass by sharing."

As has been said, while pass-by-sharing uses pass-by-value technically, the value is a reference. It is sort of a mix of pass-by-value and pass-by-reference. It isn't as helpful to call it pass-by-value as it is to call it pass-by-reference because it behaves like the latter, despite technically being a form the former. But because it isn't just pass-by-value, it is unhelpful to call it that. "Pass-by-sharing" is the correct term and tells the full story.

from 33-js-concepts.

MariusMeiners avatar MariusMeiners commented on May 2, 2024 1

indeed pass by sharing is the correct term as mentioned in my original comment.

from 33-js-concepts.

Ronnehag avatar Ronnehag commented on May 2, 2024

Objects and Arrays are passed by reference. As in, passing in an object or an array in a function will alter the reference object as well. But if you pass in an integer and do something with it inside the function, it won't get altered outside.

let i = 10;
console.log("Init value", i);

// Changing the value of i inside the function
function changeVal(i){
	i = 20;
  console.log("Inside function", i);
}
// Invoke function, passing in i as value
changeVal(i);

// Printing the value of i after the function.
// i is still equal to 10.
console.log("After function call", i);
// =========================================

// Create a new obj, prop name = "John".
let obj = {name : "John"};
console.log("Init value", obj.name);

// Changing the name of the property inside the function
function changeName(obj){
	obj.name = "Brad";
  console.log("Inside function", obj.name);
}
// Invoking the function with the object.
changeName(obj);

// Printing name prop after the function.
// The name is now "Brad".
console.log("After function call", obj.name);```

from 33-js-concepts.

MariusMeiners avatar MariusMeiners commented on May 2, 2024

I have to disagree. In a pass by reference language the folling code would alter the original object:

let foo= new Object();
foo.name = "foo";

function changeObj(obj){
  obj = new Object();
  obj.name = "bar";
}

changeObj(foo);
console.log(foo.name);
//foo

However in JS this is not the case because obj is merely a variable that holds a value (the reference) and is not the actual reference itself. Its just confusing that javascript has called them references but that doesnt change the underlying principle here. When you do a pass by reference in c++ foo would hold the new Object here.

from 33-js-concepts.

Ronnehag avatar Ronnehag commented on May 2, 2024

That is correct.
It's passed as a copy of the reference, so you could still alter the original object within a function and it would be altered outside of it.

But if you re-assign it you are just changing the copy of the reference, so then it won't be available outside of the function.

By changing it inside the function you are just telling the obj to point at a new reference and then set that name to "bar", however that scope dies and so does that new reference point.

In short, I agree. It's not really passed by reference, it's passed as a copy of the reference.

from 33-js-concepts.

MariusMeiners avatar MariusMeiners commented on May 2, 2024

saying

It's passed as a copy of the reference

is very misleading. Again: no copy is made at any point. the only thing that happens is when you write the dot operator (.) is that javascript's interpreter will perform a get - more information here and here . Then you can change whatever that object contains using set.

from 33-js-concepts.

MariusMeiners avatar MariusMeiners commented on May 2, 2024

it is passed by value every time. No exception and no copies. The only thing that is confusing is that the value that is passed is a reference itself. However pass by reference means something very different.

from 33-js-concepts.

Ronnehag avatar Ronnehag commented on May 2, 2024

The correct term is "call by sharing" or "pass by sharing."

Perfect! That was the term I was looking for, thanks for sorting it out :)

from 33-js-concepts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.