tags:

views:

2788

answers:

9

The primitive types (Number, String, etc.) are passed by value, but Objects are unknown, because they can be both passed-by-value (in case we consider that a variable holding an object is in fact a reference to the object) and passed-by-reference (when we consider that the variable to the object holds the object itself). Although it doesn't really matter at the end, I want to know what is the correct way to present the arguments passing conventions. Does anybody has a proof of this aside from the Google results?

A: 

Check here....

You cant look at the examples provided in search results and see for yourself?

StingyJack
He already said he wanted a more definitive and/or comprehensive answer than what Google provides. Pointing people at Google seems to defeat the purpose of Stock Overflow, which is to be one-stop-shopping for answers to programmers' questions.
Grant Wagner
And very interesting how you make an answer you know is incomplete a community wiki to avoid the downvotes affecting your reputation.
Grant Wagner
yeah... I cant undo it now even if I wanted to. dang.
StingyJack
@Grant: actually, the purpose of Stock Overflow is to fill your kitchen with the wonderful smell of scorched stock...
Shog9
+1  A: 

There's some discussion about the use of the term "pass by reference" in JS here, but to answer your question:

A object is automatically passed by reference, without the need to specifically state it

(From the article mentioned above.)

Jack Sleight
If you got that from google, he doesnt care about it!
StingyJack
+8  A: 

The variable doesn't "hold" the object, it holds a reference. You can assign that reference to another variable, now both reference the same object. It's always pass by value (even when that value is a reference...).

There's no way to alter the value held by a variable passed as a parameter, which would be possible if JS supported passing by reference.

Shog9
+2  A: 

Javascript is always pass-by-value, everything is a value type. Objects are values, passed as arguments, member functions of objects are values themselves and are passed by value, remember that functions are first-class objects in Javascript. Also, regarding the concept that everything in Javascript is an object, this is wrong, strings, numerics, bools, nulls, undefineds are primitives, on occasion they can leverage some the member functions and properties inherited from their base prototypes but this is only for convenience, it does not mean that they are objects themselves. Try the following for reference

x = "test";
alert( x.foo );
x.foo = 12;
alert( x.foo );

In both alerts you will find the value to be undefined.

Michael Roberts
Pass 5 into the function then call theParam.toPrecision(4), and it works. This means that it is an object. If it is able to use anything from its prototype, then it must be an object. It's not like Ruby in that you can do 5.someMethod(), but every variable is wrapped in an appropriate object.
geowa4
It's not an object to start, but when a method is called on it, it gets wrapped.
geowa4
The wrapper is discarded once the invocation is complete however, it is purely a convention and does not actually replace the original string with an object.
Michael Roberts
I don't know Ruby, but in js you can do (5).someMethod()
KooiInc
-1, it is not always pass by value. From MDC: "If you pass an object (i.e. a non-primitive value, such as Array or a user-defined object) as a parameter, a reference to the object is passed to the function."
Nick
+1  A: 

An easy way to determine whether something is "pass by reference" is whether you can write a "swap" function. For example, in C, you can do:

void swap(int *i, int *j)
{
    int t;
    t = *i;
    *i = *j;
    *j = t;
}

If you can't do the equivalent of that in Javascript, it is not "pass by reference".

Ross
Matt Greer
A: 

Primitives are passed by value and objects are passed by reference. This is quite different from other languages like C, VB or Delphi. I can't say how they handle objects and primitives exactly, but I know of VB and Delphi that it can (and should) be specified.

php does something similar since version 5: all objects are passed by reference, but all primitives may be passed by reference, if preceeded by an ampersand (&). Otherwise primitives are passed by value.

So in javascript, if I pass an object X into a function via a parameter, it will still be X. If you are changing data inside the function (or any other object, but that's not important) that new value is also available outside the function.

+5  A: 

In JavaScript - Objects are passed by reference. Primitive Types are passed by value. Here are the scripts to prove that:

Primitive Type (by value) Example

function TryChange (extra) 
{ 
   alert("in TryChange...");
   alert("in method: " + extra); //55
   extra=72; //72
   alert("after method changed: " + extra) //72
} 
var x = 55; 
alert("Before method call: " + x); // 55
TryChange(x);  //55
alert("after method call: " + x); //still 55

If you copy the following script as one continuous line of text and paste it into your browser's Navigation Bar (where URL usually goes) and then hit [ENTER] you can see the script run. (Note: You have to be on a valid web page -- like this one -- for this to work)

javascript: function TryChange (extra) { alert("in TryChange...");alert("in method: " + extra);extra=72; alert("after method changed: " + extra)} var x = 55; alert("Before method call: " + x); TryChange(x); alert("after method call: " + x);

Object (by reference) Example

function TryChange (extra) 
{ 
  alert("in TryChange..."); 
  alert("in method: " + extra.x); //55
  extra.x=72; // 72
  alert("in method, after changed: " + extra.x) //72
} 
var myObj = new Object(); 
myObj.x = 55; 
alert("Initial value: " + myObj.x); //55
TryChange(myObj); // 55
alert("after method call: " + myObj.x); // changed to 72

If you copy the following script as one continuous line of text and paste it into your browser's Navigation Bar (where URL usually goes) and then hit [ENTER] you can see the script run. (Note: You have to be on a valid web page -- like this one -- for this to work)

javascript: function TryChange (extra) { alert("in TryChange...");alert("in method: " + extra.x);extra.x=72; alert("in method, after changed: " + extra.x)} var myObj = new Object(); myObj.x = 55; alert("Initial value: " + myObj.x); TryChange(myObj); alert("after method call: " + myObj.x);

daylight
A: 

As Shog9 says, it's interesting in Javascript.

Consider this example:

function changeStuff(num, obj1, obj2)
{
    num = num * 10;
    obj1.item = "changed";
    obj2 = {item: "changed"};
}

let num = 10;
let obj1 = new Object();
obj1.item = "unchanged";
let obj2 = new Object();
obj2.item = "unchanged";
changeStuff(num, obj1, obj2);
debug(num);
debug(obj1.item);    
debug(obj2.item);

This produces the output:

10
changed
unchanged

If it was pure pass by value, then changing obj1.item would have no effect on the obj1 outside of the function. If it was pure pass by reference, then everything would have changed. num would be 100, and obj2.item would read "changed".

Instead, the situation is that the item passed in is passed by value. But the item that is passed by value is itself a reference.

In practical terms, this means that if you change the parameter itself (as with num and obj2), that won't affect the item that was fed into the parameter. But if you change the INTERNALS of the parameter, that will propagate back up (as with obj1).

deworde