How to build a reference to a global variable

J

johkar

This is a bit tricky to describe, but here goes. Any help
appreciated. John

I declare multiple variables halfway down the page. The number
between "mech" and "Num" is generated dynamically in the XSL. Later
on I need to reference that variable in a function, but I need to
build the variable name in script. How do I convert it from a string
reference to a ref to the global variable?


function changePreference(pos,mt){
//mt does not reference mech2Num, I need it to.
}
changePreference(2,'my' + pos + 'Num');


var mech1Num=1|2|3|4|5|6;
var mech2Num=1|2|3|4|5|6;
var mech3Num=1|2|3|4|5|6;
 
R

Randy Webb

johkar said:
This is a bit tricky to describe, but here goes. Any help
appreciated. John

I declare multiple variables halfway down the page. The number
between "mech" and "Num" is generated dynamically in the XSL. Later
on I need to reference that variable in a function, but I need to
build the variable name in script. How do I convert it from a string
reference to a ref to the global variable?


function changePreference(pos,mt){
//mt does not reference mech2Num, I need it to.
}
changePreference(2,'my' + pos + 'Num');


var mech1Num=1|2|3|4|5|6;
var mech2Num=1|2|3|4|5|6;
var mech3Num=1|2|3|4|5|6;

So you want "mech" + # + "Num" as a dynamic variable name? Simple:

window["mech" + # + "Num"]

Will give you a reference to it.
 
J

johkar

Randy Webb said:
johkar said:
This is a bit tricky to describe, but here goes. Any help
appreciated. John

I declare multiple variables halfway down the page. The number
between "mech" and "Num" is generated dynamically in the XSL. Later
on I need to reference that variable in a function, but I need to
build the variable name in script. How do I convert it from a string
reference to a ref to the global variable?


function changePreference(pos,mt){
//mt does not reference mech2Num, I need it to.
}
changePreference(2,'my' + pos + 'Num');


var mech1Num=1|2|3|4|5|6;
var mech2Num=1|2|3|4|5|6;
var mech3Num=1|2|3|4|5|6;

So you want "mech" + # + "Num" as a dynamic variable name? Simple:

window["mech" + # + "Num"]

Will give you a reference to it.

Thanks, I hadn't come across that method, I ended up using eval(). I
need to look up which is supported by more browsers.

John
 
M

Michael Winter

[snip]
window["mech" + # + "Num"]

Will give you a reference to it.

Thanks, I hadn't come across that method,

It's given a lot of detail in the FAQ notes
( said:
I ended up using eval().

Don't. The eval function is slower and tends to lead to errors and bad
practice. Most people start seeing it as some kind of magic-wielding,
catch-all function that will accomplish whatever the author wants. It
actually has a specific use that most people will *never* need.
I need to look up which is supported by more browsers.

Both just as well, but bracket notation is *much* preferred.

Mike
 
R

Richard Cornford

Michael said:
johkar wrote:

Both just as well, but bracket notation is *much* preferred.

They are not quite equally supported. ECMA 327 ('ECMAScript Compact
Profile') allows implementations for small devices to have an - eval -
function that does no more than throw exceptions, and the AvantGo 3
browser (for PDAs, and getting on a bit now) did not implement - eval -
at all.

Richard.
 
M

Michael Winter

Michael Winter wrote:
[snip]
Both [eval and bracket notation work] just as well, but bracket
notation is *much* preferred.

They are not quite equally supported.

[snip]

I didn't know that, but then again, I've never read the Compact Profile,
nor have I ever used AvantGo.

Thanks for the correction, particularly as it serves to reaffirm the
latter part of the sentence.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top