PDA

View Full Version : pass by reference


Endeavor88
03-16-2005, 10:55 PM
how do i pass by reference using javascript functions?

i thought using the &, but it doesn't seem to work..

IKLOP
03-16-2005, 11:17 PM
in c++, you do use & to pass by reference. however, in javascript there is no way to tell a ***ntion to pass by reference or by value. any variables that are not objects will always be passed by value and variables that are objects are always passed by reference. just one of the disadvantages of javascript.

Jon Hanlon
03-17-2005, 07:13 PM
just one of the disadvantages of javascript.


One of the advantages I'd say!

If you want to pass a primitive by reference, wrap it in an Array or Object.

Why would you want to complicate things by specifying whether a parameter is passed by Value, Result, Reference, Name, Descriptor or whatever?

Endeavor88
03-17-2005, 07:54 PM
so if i pass an object or an array to a function, javascript always interprets the parameter as a global variable??

2nd question, is there a "global" keyword in javascript?

Jon Hanlon
03-17-2005, 08:09 PM
Variables are global if they are declared outside a function, or inside a function without the prefix var.


function myVars() {
age = 22; // global (lacking var keyword)
var height = 66; // local
}

qaz = "Hi"; // global
var qwerty = 99; // global (we are not in a function)

function obbies() {
win = window; // global object that references the window
var doc = document; // local object that references the document
doSomethingTo(doc);
var myObj = new Object(); // a local object
myObj.title = "Howdy";
doSomethingTo(myObj); // operates on the local object
}

alert(typeof(win)) // results in "[object]"
alert(typeof(doc)) // results in "undefined"
alert(typeof(myObj)) // also "undefined"

function doSomethingTo(obj) {
obj.title = "New!!"
}




so if i pass an object or an array to a function, javascript always interprets the parameter as a global variable??


It depends. If you pass doc as a parameter to the doSomethingTo function, then the document's title will change, as doc is a (local) reference to the document. But you can also pass a local Object (myObj) to a function.

What are you trying to do?

IKLOP
03-17-2005, 08:26 PM
Originally posted by Jon Hanlon
One of the advantages I'd say!

If you want to pass a primitive by reference, wrap it in an Array or Object.

Why would you want to complicate things by specifying whether a parameter is passed by Value, Result, Reference, Name, Descriptor or whatever?The function determines whether a variable is passed by reference or by value, so you only need to add/remove an ampersand if you want/don't want to pass by reference. Having to wrap a variable in an Object before you send it to a function, in my opinion, is much more complicated. You need to do additional work outside of a function just to make the function work properly, and if you forgot that extra work you'll have a bug that won't give an error and might be hard to find.