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 {}.
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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With