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?
You cant look at the examples provided in search results and see for yourself?
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.)
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.
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.
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".
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.
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);
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).