4

I am relatively new to Javascript and working on a big project currently written exclusively on js. One of the concept I have read is

Passing in an object, passes it in by reference.

Following code seem to defy the rule that js passes reference in case of objects.

var a = {b:2};
var modify = function(a) {a = {d:4}};
modify(a);
print a; //a is still {b:2}.

Why has the value of a in the above example hasn't changed?

Note: It is mentioned in http://snook.ca/archives/javascript/javascript_pass that objects are passed by reference in Javascript.

3
  • The only kind of object values possible in JavaScript are references, in all cases. Commented Apr 25, 2014 at 12:57
  • 4
    The terminology is confusing. The term pass-by-reference has a specific meaning with regard to function parameters. Commented Apr 25, 2014 at 12:58
  • That snook.ca blog post is legendarily wrong. Commented Apr 25, 2014 at 13:14

3 Answers 3

6

Passing in an object, passes it in by reference.

No. JavaScript is always pass-by-value. If you pass an object, then the value is a reference to the object. If it was pass-by-reference, you would get a reference to the variable that is passed into the function.

You can mutate the object itself by adding, removing or changing properties, but changing the value of the parameter doesn't magically change the value of the passed variable.

Example:

var a = {b:2};
var modify = function(a) { 
  delete a.b;
  a.d = 4;
};
modify(a);
print a;

tl;dr: You can never change the value of a variable a by assigning a different value to a variable b.

Sign up to request clarification or add additional context in comments.

5 Comments

In the wiki link you have given. It says "anything passed into a function call is unchanged in the caller's scope when the function returns." but actually you are able to mutate the object that is passed. Isn't it. The behavior with object being passed as parameter is different from primitive values. Why would you call this pass by value.
"The behavior with object being passed as parameter is different from primitive values." No it's not, at least you cannot say it is (or isn't). The difference is that all primitive values are actually immutable. Regarding "anything passed into a function call is unchanged in the caller's scope when the function returns.", I can see what you mean. If you look further down the page, there is another section about "call-by-sharing" which this behavior seems to be called. However, it's still "call-by-value" essentially (as far as I understand).
There is no way to observe any kind of difference between passing a primitive and passing an object. Even at implementation level they are passed exactly the same way, so that would be quite the feat.
@Esailija : What I meant was that when a string or a number is passed as parameter by the caller, callee cannot really change the value because of them being immutable. I now get it.
@raju ftr, you can make an object similarly immutable just by calling Object.freeze on it
6

Nothing in JavaScript is passed by reference, it's easy to see this by attempting to implement swap:

var a = 3;
var b = 5;
swap(a, b);
// Implement swap so that a is now 5 and b is now 3
// It is impossible.

8 Comments

yes it is. var a = {}; (function(v) { v.b = 2})(a); console.log(a);
@DoXicK: You are mutating the object itself, you don't change the value of a.
@DoXicK that's not what pass-by-reference means.
@FelixKling you are right. your answer explained what i meant quite extensively. Although this answer is correct, it did not provide the depth of explanation for passing objects. Just wanted to make clear that it doesn't pass a clone of an object either.
@A1rPun: It is impossible in JavaScript to implement a function swap such that the values of the variables a and b would be exchanged, by just passing the variables as parameters. In C you could do that with pointers, in PHP with references.
|
0

Your function is just setting the local pointer, to change the referenced object, you have to do this by calling its properties and methods.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.