0

Objects are passed around by reference. They are never copied.

I have a code segment as following:

var person = { firstname: 'John', lastname: 'Smith' }
var anotherPerson = person

anotherPerson.nickname = 'Curly'
console.log(person.nickname)
// "Curly"

var fname=person.firstname
console.log(fname)
// "John"

person.firstname = 'Tom'
console.log(anotherPerson)
// Object { firstname: "Tom", lastname: "Smith", nickname: "Curly" }

console.log(fname)
// "John" <-- fname is not updated

My question is after I updated the object person's firstname to Tom, why the local variable fname is not updated?

Dave Mackey
  • 4,306
  • 21
  • 78
  • 136
YuMei
  • 687
  • 8
  • 19

5 Answers5

5

Values in Javascript are never passed by reference, always by value. Those values themselves are references in case of objects.

The fact that even objects are passed by value should be clear if you consider that a function receiving person as an argument can only change its content, it can't replace the whole value of the person variable. That's the same for fname, with the additional limit that strings are immutable which means that a function receiving the value of the fname variable can't change this variable at all.

Dave Mackey
  • 4,306
  • 21
  • 78
  • 136
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
1

It is passed by value, not by reference.

zoran404
  • 1,682
  • 2
  • 20
  • 37
0

Objects are passed around by reference.

  1. "Objects" are not values and cannot be "passed".
  2. Nothing is passed or assigned by reference.
newacct
  • 119,665
  • 29
  • 163
  • 224
-1
 var fname=person.firstname; // here fname is a string, not an object

Any update you do to person won't be reflected in fname, since it is a primitive type, and not an object.

Boundless
  • 2,444
  • 2
  • 25
  • 40
  • Not really, it's because the string is immutable. You'd have the same limit if firstname was defined as `new String('John')`. – Denys Séguret Jun 27 '13 at 20:29
-1

Contrary to some of the other answers, it has nothing to do with whether the types are primitive or not.

Taking it step by step (with irrelevant lines removed), using --> to denote 'refers to':

var person={firstname:'John', lastname:'Smith'} // person.firstname --> "John"
var fname=person.firstname // fname --> "John"
person.firstname = 'Tom' // person.firstname --> "Tom"
console.log(fname) // fname still --> "John"

If you then did:

fname = 'Bob'

Then fname would now refer to 'Bob'. fname is a reference you created that refers to the object referred to by person.firstname. Changing what person.firstname refers to doesn't change what fname refers to.

Darren
  • 2,888
  • 1
  • 23
  • 34
  • "fname is a reference you created that refers to the object referred to by person.firstname" No, `fname` is just a string primitive! It contains a copy of the string that was in person.firstname at the moment of the assignment. – bfavaretto Jun 27 '13 at 20:32
  • fname is a variable. It is a pointer (aka reference) to a string literal. Assignment DOES NOT create a copy! It simply creates a new pointer/reference to the data. Changing what a variable is assigned to has no effect on any other variables. – Darren Jun 28 '13 at 08:24
  • Are you talking about how variables are implemented by js engines? If so, you might be correct (I don't know the details), but in this case it's not relevant for understanding how the language behaves. In js itself, there are no pointers, and variables "contain" values. Some values are primitives, other values are references (to objects). – bfavaretto Jun 28 '13 at 16:43