Click to See Complete Forum and Search --> : declaring by refrence


Dudsmack
11-11-2003, 12:19 AM
Is there a way i can declare a variable by reference? How does one differintiat between declaring by value and by refrence?

Gollum
11-11-2003, 05:47 AM
You don't really get the choice. In javascript the value/reference flavour is determined by type. Primitive types like booleans and numbers are handled by value and Reference types (strangely enough) are handled by value.

There is a way to turn primitive types into Reference types by using the new operator...

var n = new Number(7);

but this doesn't help much as the wrapper classes like Number don't offer any way to modify the value.

If you did want primitive types passed by reference, you could design your own class. Sort of like...

function MyNumber(n)
{
this.value = n;
}


then any function that takes your number can modify the .value property and have the effect go back to the caller.