Does javascript support objects by reference or by value?

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 eg // myObjects[44] ? obj.addItem(result.id, result.name, result.number); } ); } 

Will the following line:

  obj.addItem(result.id, result.name, result.number); 

change the object by value or by reference, that is, it will change the local copy of obj or, for example, myObjects[44] ?

If this affects only the local copy, how can I change the global copy of the object?

+4
source share
3 answers

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

Source and further reading:

+4
source

JavaScript is passed by value, as explained in the previous question. (Someone with more authority should mark this as a duplicate - the answers here are incorrect.)

+1
source

all nonobject variables are pass-by-value afaik ..

0
source

Source: https://habr.com/ru/post/1301354/


All Articles