4

I don't know why in the following example it is not possible to define null to the object, however it is possible to add properties to it

function callByReference(myFunc) {
  myFunc.b = 2;
  myFunc = null; // this not set null to Object arg

  // myFunc.b = 2; //If I set it after, throws error
}


let customObj = {
  a: 1
};

console.log("Before call by reference method");
console.log(customObj);

callByReference(customObj);

console.log("After call by reference method");
console.log(customObj);

Even if I set it null first and then adding a property throws an error;

I don't understand this behavior well. Is there any reason? maybe I'm not understanding how javascript works when passing by reference a argument

Hans Felix Ramos
  • 4,264
  • 3
  • 16
  • 40

3 Answers3

3

The way Javascript works, every variable name is essentially a pointer (or reference) to some value. When you create a new variable by referencing an old object, or when you call a function, you're basically copying the pointer. Eg:

const fn = (param) => {
  // ...
};
const obj = {};
const obj2 = obj;
fn(obj);

Above, obj2 and param both point to the empty object that was initially created in obj, you might think of it as:

obj: MemAddress1234
obj2: MemAddress1234
param: MemAddress1234

Whenever you use the = assignment operator, you reassign the binding for that variable name, but doing so doesn't affect any other variables that may have pointed at the same thing:

param = null;

results in something like

obj: MemAddress1234
obj2: MemAddress1234
param: MemAddress0

At least, that's one way to look at it. The link between obj and the empty object, and the link between obj2 and the empty object, remain unaffected, because it's only the param variable name was reassigned.

Reassigning a variable by itself will almost never have any side-effects on anything else. The two exceptions are with arguments in sloppy mode:

function foo(a, b) {
  console.log(arguments);
  a = 5;
  console.log(arguments);
}

foo(1, 2);

and with mutable variables exported by ES6 modules.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
2

This is the way the JavaScript works:

Parameter is always pass by value, but when a variable refers to an object, the "value" is a reference to the object.

Changing the value of a variable never changes the underlying primitive or object, it just points the variable to a new primitive or object (but if you change a property a property in underlying object is changed too).

michal.jakubeczy
  • 8,221
  • 1
  • 59
  • 63
2

First, you created a customObj object, that is stored somewhere in the memory.

Then you have a function with an argument function callByReference(myFunc).

If you call the function with your object callByReference(customObj) it will assign a reference pointing at customObj to the myFunc. So now myFunc points to the same place in memory as customObj.

Now, if you modify something inside myFunc, you are changing the same memory as customObj - that is why myFunc.b = 2; will modify customObj. But if you assign something new to the myFunc, you are modifying where it is pointing, not what is inside. So myFunc = null; is like telling "now myFunc points to null memory", but customObj is still pointing to the same part of the memory, so the data is the same.

KingGary
  • 628
  • 6
  • 16