473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to build a reference to a global variable

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;
Jul 23 '05 #1
7 1970
johkar wrote:
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.

--
Randy
Jul 23 '05 #2
Randy Webb <Hi************@aol.com> wrote in message news:<7p********************@comcast.com>...
johkar wrote:
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
Jul 23 '05 #3
On 2 Nov 2004 03:55:41 -0800, johkar <no********@msn.com> wrote:
Randy Webb <Hi************@aol.com> wrote in message
news:<7p********************@comcast.com>...
[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
(<URL:http://www.jibbering.com/faq/faq_notes/square_brackets.html>).
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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #4
Michael Winter wrote:
johkar wrote:

<snip>
I need to look up which is supported by more browsers.


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.
Jul 23 '05 #5
On Tue, 2 Nov 2004 20:58:21 -0000, Richard Cornford
<Ri*****@litotes.demon.co.uk> wrote:
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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #6
Thanks. Good info. John
Jul 23 '05 #7
JRS: In article <cm*******************@news.demon.co.uk>, dated Tue, 2
Nov 2004 20:58:21, seen in news:comp.lang.javascript, Richard Cornford
<Ri*****@litotes.demon.co.uk> posted :
ECMA 327 ('ECMAScript Compact
Profile')


<FAQENTRY> ECMA 327

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: hello smith | last post by:
I have a lot of functions that add values to an array. They alos update a global variable of type int. Currently, I use a global variable to hold this array. All functions access this array...
18
by: man | last post by:
can any one please tell me what is the diff between pointer and reference.....and which one is better to use ....and why???????
3
by: Pierre | last post by:
Hello, In an aspx page (mypage.aspx) from a web projet, I would like to get the value of a variable of the projet that is declared as public in a module. The variable can be called from...
2
by: David W. Fenton | last post by:
I think at various times we've all encountered this problem: A subform is on a main form. From the code of the main form we refer to some property of/control on the child form thus: ...
12
by: John Henry | last post by:
Hi list, Just to make sure I understand this. Since there is no "pointer" type in Python, I like to know how I do that. For instance, if I do: ...some_huge_list is a huge list...
0
by: Jeff Blaine | last post by:
We have a GCC in /usr/rcf that was not built with --disable-shared. As such, there is a /usr/rcf/lib/libgcc_s.so. We also have a (preferred) GCC in /afs/rcf/lang/gcc/current that was built with...
10
by: ravi | last post by:
Hi, i am a c++ programmer, now i want to learn programming in c also. so can anybody explain me the difference b/w call by reference and call by pointer (with example if possible).
6
by: lisp9000 | last post by:
I've read that C allows two ways to pass information between functions: o Pass by Value o Pass by Reference I was talking to some C programmers and they told me there is no such thing as...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.