tags:

views:

88

answers:

3

When i started learning function in C++ its all around pass by value and reference. Is there something similar we have in javascript ?

If yes/not how it works in case of javascript?

Thanks all.

+2  A: 

A good post explaining pass by reference vs. pass by value in JavaScript.

Essentially: primitives are passed by value, objects are passed by reference.

Hooray Im Helping
That's not strictly true (and I remember thinking that the last time I read the article), all variables are passed by value but in the case of objects, the value is a reference to the object. The difference is that when a variable is passed by reference, the original variable can be changed by modifying the new one. In Javascript, even if the variable has a reference to an object, if the value of the variable changes the original object remains unaffected, although changed properties on that object will affect all references.
Andy E
I agree with @Ande E's head and I think the author of that article is simply wrong.
Pointy
+3  A: 

JavaScript is always pass by value, never pass by reference. A lot of people confuse this because of the way objects work.

There is no "pass by reference" for any variable in JavaScript (no, not even if an object is assigned to that variable). All variables and arguments are assigned by value. If the assigned value is an object, then the value of the new variable is a reference to that object, but assigning a new value/object to the new variable will not affect the original variable.

http://blogs.devsource.com/msdev/content/ajax_and_client-side/javascript_pass_by_reference_or_value_1.html

A comparison - PHP

$foo = "foo";
$bar = &$foo;  // pass by reference
$bar = "bar";
echo $foo; // -> output: "foo"

JavaScript

foo = {"foo": true};
bar = foo;
bar = {"bar": true};
alert(JSON.stringify(foo)); // -> output: '{"foo": true}
Andy E
For all intents and purposes, objects (including arrays) are passed by reference. Saying that "you are passing by value, but the value is a reference to the object" is another way of saying "you are passing by reference". Unless you are just trying to confuse people.
rob
@rob, not for **all** intents and purposes. For example, many WMI scripting calls require that you pass a variable to the function by reference, so that the return value of that function can be an error number. Instead of returning the result, it assigns the result to the variable passed in. VBScript can handle this just fine but JScript/JavaScript cannot because it cannot pass a reference to the variable in. They are not the same thing and I'm not trying to confuse people, I'm trying unconfuse people by expelling a myth that's already caused a lot of confusion.
Andy E
No, @rob, that's not the case. The *value* that is passed is the value of the reference. In a true pass-by-reference language, the compiler introduces a pointer that's invisible in the code. In a pass-by-reference language you can write the notorious "swap two ints" function naturally. The value of a variable in Javascript is *always* a reference to an object, and when you call a function you're *always* passing that value. If Number had methods on it that allowed the value to be changed, it would be more clear, perhaps.
Pointy
@Andy E's head, the example you give works fine in Javascript if it is an object, but not if it is a primitive, just as I said. Example: for a function that multiplies a number (int or float), and you've got to return the result as a value rather than modifying the number and using the error message for whatever. Now try doing the same, modifying a point object, which has an x and a y: function multiplyPoint (point, factor) { if (isNan(factor) return -1; point.x *= factor; point.y *= factor; return 0; }
rob
@rob: Of course it will work, you misunderstood the answer and the comments. The value of the point variable is a reference to the object, but point itself is not a reference to the variable that was passed in. Pass by reference is different in the way that if you set `point = "a string and not an object";` inside the function, the original variable that was passed to the function would be modified. In JavaScript this is not the case. Pass by reference and pass value by reference are not the same thing.
Andy E
Obviously we won't agree on this, I understand what you are saying but don't think it serves most people to view it that way. Crockford considers Javascript a "pass by reference language" (http://developer.yahoo.com/yui/theater/video.php?v=crockonjs-5), and javascript's handling of objects is pretty much identical to C++ when passing by reference, and it's handling of primitives is pretty much identical to C++'s when passing by value. I'm sure we could debate it forever, but this will be my final word. :)
rob
@rob: we'll agree to disagree then ;-) I honestly think it's confusing to call it a pass by reference language, when you work with some other languages that pass by reference and particularly in this case extra clarity is needed to understand the difference. Final word ;-)
Andy E
Andy E
+2  A: 

Other answers to this question are correct - all variables are passed by value in JavaScript, and sometimes that value points to an object.

When a programming language supports passing by reference, it's possible to change where the variable points from inside a function. This is never possible in JavaScript.

For example, consider this code which attempts to reassign the passed variable to a new value:

function changeCaller( x ) {
    x = "bar";  // Ha ha!
}

function testChangeCaller() {

    var x = "foo";

    changeCaller(x);

    alert(x); // still "foo"

}

testChangeCaller();

Attempts to point variables at new objects fails in the same way the above example fails to reassign a primitive string:

function changeCaller( x ) {
    x = new Object(); // no longer pointing to the same object
    x.a = "bar";
}

function testChangeCaller() {

    var x = new Object();
    x.a = "foo";

    changeCaller(x);

    alert(x.a); // still "foo"

}

testChangeCaller();

The feature which leads people to believe they're dealing with a pass-by-reference scenario is when modifying objects. You can make changes to an object or array, because the local variable points to the same object:

function changeCaller( x ) {
    x.a = "bar";
}

function testChangeCaller() {

    var x = new Object();
    x.a = "foo";

    changeCaller(x);

    alert(x.a); // now "bar", since changeCaller worked on the same object

}

testChangeCaller();

Hope this helps!

jimbojw