...

View Full Version : Pass-by-Reference in JS(Disscusion)



Code Wizard
01-11-2004, 09:42 AM
OK,I have a few questions on this:
Objects are always passed by reference,that is,if they are passed as function arguments,or assigned to a variable(ex:myobj=document)
But why are functions also passed by reference?
For example:funcRef=myfunction....and then I can call funcRef(3,4)
I know that for each function created,a function Object is also created,but I though the function data type was distinct from teh object data type,no??

Also,I believe it's possible to pass a variable(that holds a primitive data type) by reference,by using the wrapper objects(Number,Boolean,String)....can someone confirm this,and show an example?
Regards.

liorean
01-11-2004, 09:54 AM
JavaScript is pass-by-reference in general, it's only the native simple types that are pass-by-value - null, undefined, boolean, string, number. All other are passed by reference. By using one of the Boolean, String, or Number types as a constructor using the new keyword you can make an object from them, which you can then return to it's base type by using the toValue method.

RoyW
01-12-2004, 04:29 PM
I have to disagree. JavaScript is pass by value only.

When you create an Object
var x= new Object()
x.value = 10;

The value 'x' is a reference to that object. When you pass 'x' to a function you pass the value of 'x' not a reference to 'x'. If it were pass by reference you would be able to do something like


function nullify(a)
{
a = null;
}
var x = new Object();
x.value = 10;
nullify(x);
alert(x) // does not alert null, will alert (Object)


However you can change the object data that the reference points to.


function increaseValue(a)
{
a.value += 10;
}
var x = new Object();
x.value = 10;
increaseValue(x);
alert(x.value) // alerts 20.

This emulate pass by reference but the object reference 'x' is actually passed by value.

See this reference http://www.webreference.com/js/tips/010210.html

liorean
01-12-2004, 08:15 PM
Nice arguments, but faulty logic. Let me explain why:
- You send x, being an object, to the nullify function. This object is there assigned to the argument variable a. You then assign the variable a a new value, null. Since you overwrite the local variable a but not the global variable x, this doesn't tell us anything at all about whether you are sending a reference or a value.
- In your second example, you are accessing a child of the object you sent to the increaseValue function. Had it been pass-by-value, we would here have seen the local variable a containing an object with the value property set to 20 while the global variable x would not have been modified. However because this is pass-by-reference, we are not sending the value of the object stored in x, only a reference to it. This means when you change a, you change the object which both x and a refers to, instead of just a.





The parallel webreference does to the C/C++ way of pass-by-reference is somewhat strange, since we are not comparing same-paradigm languages. In C/C++ values are always hard structures, and references are memory addresses. JavaScript is on the other hand object based - which means that in JavaScript everything, both primitive values and objects, are internal objects. Primitive values are stored as hard structures in properties of those internal objects, while objects are stored as references to the hards structures in properties of the internal objects. So, strictly speaking, JavaScript is pass-by-reference to those internal objects, and pass-by-value if if you look externally. However; since it is the hard structure that is the property of the internal objects for a primitive, and it is a reference to the hard structure representing the object that is the property of the internal object for objects; objects are effectively from an external point of view pass-by-reference in JavaScript while primitives are pass-by-value.

The primitive types are Undefined, Null, String, Number, Boolean. All other types, except for possible host objects, are objects.

RoyW
01-12-2004, 08:47 PM
If JavaScript is pass by reference could you tell me how to implement a swap function. This certainly wouldn't work


function swap(a, b)
{
var t;
t = a;
a = b;
b = a;
}

var v = new Object()
var w = new Object();
var x = new Object()
var y = new Object();

v.value1 = 10;
v.value2 = 11;

w.value1 = 20;
w.value2 = 21;

x.value1 = 30;
x.value2 = 31;

y.value1 = 40;
y.value2 = 41;

swap(v, w);// Swap v and w
swap(x, y);//Swap x and y



however, in C++ if you defined the function to accept pass-by-reference values (&a, &b)


void swap(objType &a, objType &b)
{
objType t;
t = a;
a = b;
b = a;
}

Then the global variables outside the function would be updated which is the definition of pass by reference.

<edit>
I think I've confused myself now :rolleyes: :)
If it were pass by value then the whole object would be copied to the stack and modifying inside the method would not modify the global object. But if (like I assumed) you consider the original 'x' as already just a reference to the object the value 'x' is passed by value but the object is passed by reference :confused: Ok, I give up ;)

RoyW
01-12-2004, 09:19 PM
Ok,
I found these 2 arguments on the WEB. I know JavaScript is not Java but they argue the same point and I think the arguments they put forward for Java hold true for JavaScript also.

http://javadude.com/articles/passbyvalue.htm
http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html

:)

liorean
01-12-2004, 10:34 PM
In the imperative paradigm, yes. Java uses wrappers (also called soft structures) the same way JavaScript does, and it does send those wrappers by value. The contents of such a wrapper is either a hard structure or a reference to an object. The reason the swap function does not work is just this. You are replacing the wrapper when you do it, you are not replacing the actual contents of the wrapper. For primitives, that means you effectively destroy them when you assign a new value. For objects, it means you just destroy a reference, you don't destroy the actual hard structure, since it was never located in the wrapper. The Java World article cites "Java in a Nutshell" by David Flanagan: "Java manipulates objects 'by reference,' but it passes object references to methods 'by value.'"

So, I concede. You are right. In fact everything is passed by value in both Java and JavaScript, but the value may be a hard structure or a reference, but is never an object. Objects are only reachable by those references, and are never a part of the value - which is why it is all but impossible to clone objects in JavaScript.

However, in practice, you are passing references - the real difference lies in the variable binding, and that all variables are separate pointers in object based languages, but that you are passing the actual pointers around in C/C++ pass-by-reference.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum