2
function myFunc(theObject) {  
  theObject = {make: "Ford", model: "Focus", year: 2006};  
}  
var mycar = {make: "Honda", model: "Accord", year: 1998};  
var x = mycar.make;     // returns Honda  
myFunc(mycar);  
var y = mycar.make;     // still returns Honda  

Why doesn't myFunc change the mycar object??

DarkLightA
  • 14,980
  • 18
  • 49
  • 57

4 Answers4

9

When you do theObject = { ... } within myFunc, you are creating a new object and assigning its reference to the local variable theObject. This does not change the original object.

To modify the contents of the original object, you need to directly modify its properties like this:

theObject.make = 'Ford';
theObject.model = 'Focus';
theObject.year = 2006;
casablanca
  • 69,683
  • 7
  • 133
  • 150
  • I don't get this. I thought that `theObject` in `myFunc` was a reference to the object that was passed in to `myFunc`, and not a local variable? – Tola Odejayi Dec 29 '10 at 19:39
  • It is, but he is assigning a new object to the local variable, not modifying the passed in object's properties. – kemiller2002 Dec 29 '10 at 19:40
  • 1
    @Tola Odejayi: `theObject` itself isn't a reference to `mycar`, it *holds* a reference to `mycar`. You can change `mycar` through `theObject`, but if you assign another object to `theObject`, it now *holds* the new reference. – casablanca Dec 29 '10 at 19:46
  • Ah... got it. So `theObject` in `myFunc` and `myCar` outside `myFunc` both point to the same object at the start of `myFunc`, but the moment we do the `new`, `theObject` now points to a **different** object. – Tola Odejayi Dec 29 '10 at 20:16
  • @Tola Odejayi: Exactly, object references are really just pointers in disguise. – casablanca Dec 29 '10 at 20:23
2

The question is already answered, just to make it even clearer:

function myFunc(theObject) {  
      theObject = {make: "Ford", model: "Focus", year: 2006};  
} 

is something similar (forget the syntax, get the message) to:

function myFunc(theObject) {  
      theObject = new TheObject("Ford","Focus",2006);  
} 

in other words, the parameter is referenced but you are changing that reference by constructing a new object.

Note: Since Java syntax is so popular I thought of using a JAVA-like syntax in order to explain, with didactic purposes, that you're creating a whole new instance. "TheObject" would be the name of the class.

Ivan Arrizabalaga
  • 686
  • 1
  • 7
  • 22
0

Javascript is modifying the local reference not the original reference when making the change you supplied. This post on SO should help:

Is JavaScript a pass-by-reference or pass-by-value language?

Community
  • 1
  • 1
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
0

Change this:

function myFunc(theObject) {  
      theObject = {make: "Ford", model: "Focus", year: 2006};  
    } 

Here you are reassigning your variable to a new object. The original is left unchanged, because parameter does not link to the variable holding the object.

to:

function myFunc(theObject) {  
  theObject.make = "Ford";
} 

This changes the object's properties you passed in.

kemiller2002
  • 113,795
  • 27
  • 197
  • 251