3

pass by reference is not working here when we assign obj2 to obj1 in a function it doesn't work outside the function and obj1 is retaining its original value but why?

let obj1 = {
  value: 'a'
}

let obj2 = {
  value: 'b'
}

obj3 = obj2;

function change(obj1, obj2) {
  obj1 = obj2
  obj2.value = 'c'
}


change(obj1, obj2);

console.log(obj1.value)
Synoon
  • 2,297
  • 4
  • 22
  • 37
Rishabh
  • 53
  • 1
  • 4

3 Answers3

5

The parameter obj1 inside the function will refer be a local variable which is referring to global variable obj1. But when you reassign obj1 the reference is removed so that's why the value is not changed and then it has no connection with the obj1 of global scope.

I just changed the names of variable to show what is going on.

let obj1gl = {
  value:'a'
}

let obj2gl = {
  value:'b'
}


function change(obj1,obj2){
  console.log(obj1gl === obj1); //true
  //this line removes the reference.
  obj1 = obj2
  console.log(obj1gl === obj1); //false
  obj2.value = 'c'
}


change(obj1gl,obj2gl);
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
  • "that's why the value is not changed and then it has no connection with the obj1 of global scope." but OP's snippet doesn't seem to attempt to change obj1. It'd be more interesting the snippet was edited to: obj1.value = 'c' – AnonEq Jul 04 '19 at 12:29
  • @AnonEq OP is clearly saying _"obj1 is retaining its original value but why?"_. It means he expects a change in `obj1`. – Maheer Ali Jul 04 '19 at 12:50
  • OP's question's language indicates they 'expects' a change in obj1. Whether their code aligns with the 'conditions for obj1 change' they seek to clarify is a different story. – AnonEq Jul 04 '19 at 12:52
1

It is a concept of local vs global scoping.

though variable names are identical but when you have passed in a function, their scope is limited to that function and so it does not affect global variables having similar names.

console.log is outside of that function which picks up unaffected global variable.

One solution could be to use global variables inside function and do not pass anything in the function else use different naming convention amongst local and global variables, then modifying global variable will work inside a function.

Kausha Shah
  • 309
  • 3
  • 9
0

JS primitives are passed by value.

JS objects are also passed by value, but the value is a copy of a reference.

//global object in this runtime env is 'window'
var obj1 = { //global scope obj1
  value: 'a'
}

var obj2 = { //global scope obj2
  value: 'b'
}


function change(obj1, obj2) { //js = "pass by copy of reference"
  obj1 = obj2              //obj1's value is now a reference (aka an address in memory) to global obj2
  console.log(typeof obj1) //logs 'object', (meaning local var obj1's value is a reference )
  obj1.value = 'c'         //global obj2 value changed.
  console.log(window.obj2) //logs 'value': 'c'
  console.log(window.obj1) //logs 'value': 'a'
}


change(obj1, obj2);

console.log(obj1.value) //logs 'a'
console.log(obj2.value) //logs 'c'
AnonEq
  • 267
  • 2
  • 9