Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass-by-reference JavaScript objects

Tags:

javascript

Please read the comments in the code below to know what I am trying to ask.

Expected Output: As per pass-by-reference mechanism in JavaScript, objOne is expected to log {} at the end, because objTwo was initialized with {}.

var objOne = {   x: 1,   y: 2 };  var objTwo = objOne;  // change the x vlaue to 2 by objTwo objTwo.x = 2;  // Change the value of key x in objOne as well - pass by reference mechanism console.log(objOne); // { x: 2, y: 2 }  /*** Pass by reference is understood in code, above this comment ***/  // Now what if objTwo initialized with empty object objTwo = {};  console.log(objOne); // { x: 2, y: 2 } but expected output = {}  // As per pass by reference mechanism. objOne is expected to log {}, because objTwo was initialized with {}. 
like image 837
Abhay Avatar asked May 18 '16 05:05

Abhay


People also ask

Can we pass objects by reference?

A mutable object's value can be changed when it is passed to a method. An immutable object's value cannot be changed, even if it is passed a new value. “Passing by value” refers to passing a copy of the value. “Passing by reference” refers to passing the real reference of the variable in memory.

Are arrays passed by reference JavaScript?

In Javascript objects and arrays follows pass by reference. so if we are passing object or array as an argument to the method, then there is a possibility that value of the object can change.

Are objects passed by reference in typescript?

Pass in Arguments by ReferenceNon-primitive are passed into a function by reference, which means that the reference to the object passed in as the argument is passed into the function. The copy of the content is not made for the argument and the passed in object is modified directly.

How the objects are passed in JavaScript?

Objects are Passed by Reference In JavaScript, object references are values. Because of this, objects will behave like they are passed by reference: If a function changes an object property, it changes the original value. Changes to object properties are visible (reflected) outside the function.


2 Answers

When you assign one variable to another, it's not that both those variables are now linked by reference; you're misunderstanding what "pass by reference" means here.

A variable holding an object does not "directly" hold an object. What it holds is a reference to an object. When you assign that reference from one variable to another, you're making a copy of that reference. Now both variables hold a reference to an object. Modifying the object through that reference changes it for both variables holding a reference to that object.

When you assign a new value to one of the variables, you're just modifying the value that variable holds. The variable now ceases to hold a reference to the object, and instead holds something else. The other variable still holds its reference to the original object, the assignment didn't influence it at all.

like image 77
deceze Avatar answered Sep 24 '22 09:09

deceze


When you evaluate

objTwo = {}; 

Javascript interprets that as reassigning objTwo to a new literal empty object, and leaves its old value alone.

If you want to remove a key from objOne by reference, you can use the delete keyword:

delete objTwo.x;  // also deletes the x property of objOne 
like image 42
Razzi Abuissa Avatar answered Sep 22 '22 09:09

Razzi Abuissa