PDA

View Full Version : Javascript support pointers?


beetle
10-08-2002, 03:48 PM
Ok, I'm pretty sure the answer is 'no', but I thought I'd ask.

Just to clarify, here's a PHP equivalent$foo = "bar";
$bar = "Hello World!";
echo($$foo); // Prints 'Hello World!'

ahosang
10-08-2002, 04:11 PM
I don't know PHP but what functionality are you trying to get?

lpok
10-08-2002, 04:18 PM
To my knowledge, you can't directly use pointers. You can however make an object and use that like a pointer.


function Int(num) {
this.value = num;
}

function addOneFromPointer(pointer) {
pointer.value++;
}


var x = new Int(6);
alert(x.value);
addOneFromPointer(x);
alert(x.value);



Java doesn't support pointers, so I doubt javascript will.

beetle
10-08-2002, 04:19 PM
Uh, let me break down my example

$foo = "bar";
Creates variable 'foo' with the the string 'bar';

$bar = "Hello World!";
Creates the variable 'bar' with the string 'Hello World!'

echo($$foo);
echo == document.write()
The $$ tells PHP to find the variable with the name that identical to the string value within it. This is called a pointer.

No offense, but having to explain this to you means you probably don't know :D

Anyone, I've got it figured out....var foo = "bar";
var bar = "Hello World";
document.write(eval(foo));Not as elegant as I'd like, but it works. Thanks just the same.

jkd
10-08-2002, 04:40 PM
Originally posted by beetle
var foo = "bar";
var bar = "Hello World";
document.write(eval(foo));

Why not just do:

document.write(window[foo]);

Then?

This really isn't pointers or references you are getting at.

beetle
10-08-2002, 04:49 PM
I know...just wanted to know if I could access a variable by the string value of another. Not sure I'm really gonna use this....more of a curiosity thing....

mordred
10-08-2002, 05:06 PM
Beetle, also your PHP example has nothing to do with pointers. That's just plain variable variables. With the object lookup operators [] you can achieve the desired effects, as jkd pointered err pointed out. ;)

beetle
10-08-2002, 07:55 PM
I see. I have apparently been previously mis-informed about the nature of pointers in PHP

thanks.

adios
10-08-2002, 08:26 PM
If you really want to get giddy - read this (http://www.unitedscripters.com/index.html?origin=http://www.unitedscripters.com/spellbinder/pointers.html) (from a former denizen of this board).

Ignore that picture on the left.

beetle
10-08-2002, 09:27 PM
Do I have to ignore it? :D

beetle
10-08-2002, 09:53 PM
Thanks for the article Adios...

I must say though, that is darn near the ugliest site I've seen. I think It's trying to be ugly. Ugliness that severe doesn't occur naturally.

:D