INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

just programming features

pass variable by its address (pointers) by baad
Posted: 2 Aug 01

hie guyz
this faq is for those people who would want something like the following:
I would like to pass a variable from one function to another function, change this variable in the second function and when returned to the first function the value would have been changed here.


well, IMHO the only objects that pass by value - local variables (correct me if i'm wrong)
you can pass a variable like a pointer (pass an address) through special functions:

1) write

function scall(func)
//call "special" function
{
var sf=func;
for(var ii=1;ii<scall.arguments.length;ii++) {
rg=new RegExp("argv\\["+ii+"\\]","g");
sf=sf.replace(rg,scall.arguments[ii]);
}
return sf;}

2) define "special" function itself:

var FUN="argv[1]++;argv[2]+=argv[1];"



you c, that it is not a function, but a string, arguments written just like in C's main()

3)checkin it:

function test(){
var A=10;
var B=20;
//arguments passed like strings
eval(scall(FUN,"A","B"));
alert("A="+A+" B="+B);
}

looks like it works.. but why bothering so much?

you could (& i think it is the best way 2 do it) make objects from your variables
function Int(x){
this.v=x;
}

function inc(p){
p.v++;
}

function test(){
var A=new Int(10);
inc(A);
alert(A.v);
}


feel free 2 tell me if something is wrong, looks like it works..
wuoah.. :)

Back to Javascript FAQ Index
Back to Javascript Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close