JavaScript pointers

Alphathree33

Platinum Member
Dec 1, 2000
2,419
0
0
I have some global variable called

var x

Now, in function A, I want to pass x to function B and have it be initialized... I don't want a COPY of x initialized, I want x initialized:

function a()
{
b( x );
}

function b( variable_to_init )
{
variable_to_init = new ActiveXObject("stuff");
}

Now as far as I can tell, x is getting passed by value or some such thing to b, and then I get the message that "x has no properties" when I try to use it elsewhere.

Is there a way to do this in JavaScript? I could show you the C++ code I'd use... :)
 

Gunslinger08

Lifer
Nov 18, 2001
13,234
2
81
First off, I'm assuming these functions are in separate classes? If not, couldn't you directly reference x in function b, without passing it from a?

Pointers are not implemented in JavaScript, as passing values by reference was never incorporated into the language. JavaScript functions do not know the physical location of their variables in memory, and it is impossible to find this location.
 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
Originally posted by: Alphathree33
I have some global variable called

var x

Now, in function A, I want to pass x to function B and have it be initialized... I don't want a COPY of x initialized, I want x initialized:

function a()
{
b( x );
}

function b( variable_to_init )
{
variable_to_init = new ActiveXObject("stuff");
}

Now as far as I can tell, x is getting passed by value or some such thing to b, and then I get the message that "x has no properties" when I try to use it elsewhere.

Is there a way to do this in JavaScript? I could show you the C++ code I'd use... :)


It wouldn't work because you can't pass object by references in Java/JavaScript. You are essentially passing pointer *value.* In function b() you are merely overwriting this pointer value that you pass from a (within b's scope) and this has no effect in function a's varriables.

What you could do is this:

function a(){
var the_initialized_x = b();
}

function b(){
var x = new ActiveXObject("stuff");
// do other stuff to x
return x;
}

or nest function b() inside function a() like this...
function a(){
var x;
b = new function(){
x = // iniitialize;
};

b();
}
but this^^^ is pointless..