-1

Is there any way to force pass by reference in javascript? I have read that objects are passed as ‘copy of object reference’ or call-by-sharing but I want the changes across the functions to be reflected across the sender and the receiver. Current design prevents me from declaring it as a global variable. Any ideas on how to handle this?

For example, considering the code below ->

enter image description here

Now when I execute function1, I get ->

enter image description here

How do I ensure that function1 has the changed/updated values?

Community
  • 1
  • 1
Jhalaa Chinoy
  • 497
  • 5
  • 14
  • 1
    Can you post your code and what you're trying to do so we can figure out how you might fix it? – CertainPerformance Oct 29 '18 at 22:44
  • 1
    JavaScript already passes objects by reference. – fubar Oct 29 '18 at 22:45
  • If they are not than no.... Either you have to edit what every code that is making the copy or live with the design patterns that the code enforces. Sounds like they want you to use a method to update what is changed. – epascarello Oct 29 '18 at 22:45
  • 3
    There is no pass-by-reference in JavaScript, point. You cannot "force" it, it doesn't exist. Store the things you need to share in objects and pass them. – Bergi Oct 29 '18 at 22:47
  • 3
    @fubar No it doesn't. Everything in JavaScript is passed by value. It's just that when you pass an object reference, you are passing a copy (pass by-value) of the reference. So ultimately, whichever reference you use, you point to the same one object. – Scott Marcus Oct 29 '18 at 22:50
  • 1
    Show your code that has the issue.... I have a feeling the issue is something different than what you think it is. – epascarello Oct 29 '18 at 22:56
  • @ScottMarcus thanks for your comment. I knew a variable stored either a primitive or a reference to an object, but I wasn't aware of the subtlety that it was actually only a copy of the object reference was passed. – fubar Oct 29 '18 at 22:59
  • I really don't understand the downvotes for this question: it is clear and valid, with efforts and minimal code - those qualities are missing in very old highly upvoted questions with outdated answers. Jeff Atwood was right. – Jan Turoň Jun 11 '23 at 17:11

2 Answers2

0

I have found this https://codeburst.io/javascript-pass-by-value-and-pass-by-reference-in-javascript-fcf10305aa9c

However, when a variable refers to an object which includes array, the value is the reference to the object

gfdevelop
  • 187
  • 2
  • 7
0

From what I understand, pass-by-reference in JS terminology means pass-by-pointer in C++ terminology, and JS doesn't have pass-by-reference in C++ terminology - and there is lot of confusion about this, because the two worlds rarely communicate: the C++ and the JS programs usually covers completely different realms.

For example, JS terminology describes pointers as copy of object reference (which doesn't make sense in C++ terminology where reference means alias). And when JS community says everything is passed by value it means the value of the pointer (primitives are boxed when passed).

There is a way to make the function1 get the updated value from myVar, though. It can be done by call-by-name in the following steps:

  1. create a context in function1
  2. create myVar in the context
  3. call the function2 in the created context and pass the name

The essence of pass-by-reference equivalent implemented as pass-by-name below is the line

function2.call(context, "myVar");

and the reference in function2 is obtained by

this[name]

as demonstrated in the code below

function1 = function() {
    var context = {};
    context.myVar = {1:"one", 2:"two"};
    console.log("function1 before:", context.myVar[1]);
    function2.call(context, "myVar");
    console.log("function1 after:", context.myVar[1]);
}

function2 = function(name) {
    console.log("function2 before", this[name][1]);
    this[name] = {1:"three", 2:"nine"};
    console.log("function2 after", this[name][1]);
}

function1();
Jan Turoň
  • 31,451
  • 23
  • 125
  • 169