1

I have a global object, and I'm trying to pass its reference into a function, and then change the object that reference points to in the function called. Here is an example of the problem with a simple 3 element array:

var myObject = [1, 2, 3]; //this is global
changeObject(myObject);

function changeObject(obj) {
  $('#beforeLocal').html( obj.toString() );
  $('#beforeGlobal').html( myObject.toString() );
  obj = [4, 5, 6];
  $('#afterLocal').html( obj.toString() );
  $('#afterGlobal').html( myObject.toString() );
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Local array before changing: <span id="beforeLocal"></span></p>
<p>Global array before changing: <span id="beforeGlobal"></span></p>
<p>Changing local array</p>
<p>Local array after changing: <span id="afterLocal"></span></p>
<p>Global array after changing: <span id="afterGlobal"></span></p>

However, when changing the local object, it only changes the local reference, and not the global one. I know that in this example,

obj[0] = 4;
obj[1] = 5;
obj[2] = 6;

would work.

I want to achieve this without cycling through all the values of the array, and also I want it to work for arrays of different sizes and/or objects in general.

I need to do this in this way because I need a general function, so I can't reference the global array directly inside the function (because it needs to work for multiple arrays) and I also cannot use the return value because this is something I need to do asynchronously (this is a simplified problem).

  • 2
    No, JavaScript is pass-by-value. You can choose *not* to pass the reference as a parameter, and then code the function to directly reference the global variable, or you can design your functions to *return* new objects, which can be assigned to something in the calling context. – Pointy Nov 18 '18 at 15:46
  • If the use case is the use the global in the function and change the global in the function, then why not just use the global within the function? Looking beyond why this is such a bad thing to do, you seem to be over complicating the issue. (and should consider never manipulating globals in the way you've described.) – Randy Casburn Nov 18 '18 at 15:49
  • Javascript is pass-by-reference when it comes to objects. I've also updated the question to add why I need to do this in the way I'm trying to do it, i.e. no referencing global variable inside function and no return values. – Filip Bacinger Nov 18 '18 at 15:53
  • If you simply need to replace the array contents just clean it and add new values. `obj.length = 0; obj.push(4, 5, 6);`. BTW js is always pass-by-value (its just the value happens to be a reference). – Yury Tarabanko Nov 18 '18 at 16:06
  • If you need it for different arrays store them in a Map or object and just pass in the key so you can do something like `allArrays[key] = [4,5,6]` – charlietfl Nov 18 '18 at 16:07
  • Thank you, these are ways of getting around the problem which I will use. But now I'm curious, is something like this not possible at all in Javascript? – Filip Bacinger Nov 18 '18 at 16:19
  • Javascript uses by value for primitive types and by reference for objects. Primitive values cannot be shared between variables. – Cue Nov 18 '18 at 16:27

0 Answers0