Does javascript handle objects by reference or by value?

javascript, jquery

Solution

Primitive variables are passed by value in JavaScript, but objects are passed by reference.

Source and further reading:

- JavaScript: Passing by Value or by Reference

Problem

I have this code ``` var myObjects = {}; //global variable //Later on in the code: for (i in myObjects) { var obj = myObjects[i]; process(obj); } function process(obj) { $.getJSON("example.com/process/", {id: obj.id}, function(result) { //Will the following change the permanent/global copy e.g // myObjects[44] ? obj.addItem(result.id, result.name, result.number); } ); } ``` Will the following line: ``` obj.addItem(result.id, result.name, result.number); ``` modify the object by value or by reference, i.e will it modify the local copy of `obj` or e.g `myObjects[44]`? If it affects only the local copy, how can I have it change the global copy of the object?

Original source

Related problems