473,503 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Q: Check existence of several global variables

Dear Experts:

Suppose I have global variables: x1, x2, x3
and I have a function that needs to assign a value to a new global variable
x4

something like
function foo ()
{
count = 0;
do {
count ++
varname = 'x'+ count
} while (globalExists (varname)

eval ( varname + ' = "I am new in the x-series" ' )
}

how would the function globalExists () be implemented ?
(The new variable would eventually be used as a setInterval arg or
eventhandler arg in generated html )

--
Richard A. DeVenezia
Jul 20 '05 #1
5 5306
"Richard A. DeVenezia" <ra******@ix.netcom.com> writes:
Suppose I have global variables: x1, x2, x3
and I have a function that needs to assign a value to a new global variable
x4
something like
(lotsa typos in this :)

eval ( varname + ' = "I am new in the x-series" ' )
You don't need eval for this. You probably never need eval at all.

window[varname] = "I am new in the x-series";
how would the function globalExists () be implemented ?
In modern browsers:
function globalExists(name) { return name in window; }

In older browsers, if you know that the value of the variable is
not undefined:
function globalExists(name) { return window[name]!==undefined; }
and even older (doesn't have "undefined" as a variable, and you haven't
made one yourself)
function globalExists(name) { return typeof window[name]!="undefined"; }
(The new variable would eventually be used as a setInterval arg or
eventhandler arg in generated html )


I guess that you are going about this the wrong way. I cannot imagine
a case where you need to generate new global variables dynamically,
where you can't get the same effect with local variables and closures
instead. Both setInterval and event handlers can use functions/closures.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote:
You probably never need eval at all.
and later...
I cannot imagine a case where you need to generate new global variables

dynamically.

Long time ago, I found one interesting article located at
http://www.webreference.com/js/column18/.
Could you please look at it, and concentrate on this function:function
animation(id) {
this.element = (NS4) ? document[id] : document.all[id].style;
this.active = 0;
this.timer = null;
this.path = null;
this.num = null;

this.name = id + "Var";
eval(this.name + " = this");

this.animate = animate;
this.step = step;
this.show = show;
this.hide = hide;
this.left = left;
this.top = top;
this.moveTo = moveTo;
this.slideBy = slideBy;
this.slideTo = slideTo;
this.circle = circle;
}What do you think about that? That function generates global variables and
uses eval. Regards.
Jul 20 '05 #3
"Vjekoslav Begovic" <vj*******@inet.hr> wrote in message
news:bi**********@sunce.iskon.hr...
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote:
You probably never need eval at all.
and later...
I cannot imagine a case where you need to generate new
global variables dynamically.


Long time ago, I found one interesting article located at
http://www.webreference.com/js/column18/.


<quote cite="http://www.webreference.com/js/column18/">
var NS4 = (document.layers) ? 1 : 0;
var IE4 = (document.all) ? 1 : 0;
</quote>

<quote cite="http://www.webreference.com/js/column18/">
function hide() {
this.element.visibility = (NS4) ? "hide" : "hidden";
}
</quote>

Scripts written in the age of "both browsers" and seriously in need of
being brought into the 21st century. Generally a bloated, inefficient
and fragile way of doing something that is relatively trivial (and many
times more cross-browser achievable than this script would suggest).
Could you please look at it, and concentrate on this function:
function animation(id) {
this.element = (NS4) ? document[id] : document.all[id].style; <snip> eval(this.name + " = this");
window[this.name] = this; //same effect, no eval, and faster!

<snip> }What do you think about that? That function generates global
variables and uses eval.


The process that the script performs requires no global variables to be
created. Indeed I cannot imagine a case where you need to generate new
global variables dynamically. The author of the script is only using
them to work around shortcomings in their knowledge of JavaScript. One
of the things that make those shortcomings self-evident is the
unnecessary use of the eval function to create the global properties,
though there are plenty of others.

The eval function is almost never (if not actually never) needed in
JavaScript.

Richard.
Jul 20 '05 #4
"Vjekoslav Begovic" <vj*******@inet.hr> writes:
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote:
You probably never need eval at all.
and later...
I cannot imagine a case where you need to generate new global variables
dynamically.

Long time ago, I found one interesting article located at
http://www.webreference.com/js/column18/.
Could you please look at it, and concentrate on this function:function
animation(id) {

this.element = (NS4) ? document[id] : document.all[id].style;
At least use "document.layers[id]" for NS4, and add support for
non NS4/IE browsers. Yes, this code is probably from back when
there were no other visible browsers, and no official standard
for accessing elements, so they are excused.

From this line, I will also allow myself to infer that they are
targeting NS4 and IE4.
this.active = 0;
this.timer = null;
this.path = null;
this.num = null;

this.name = id + "Var";
eval(this.name + " = this");
Eval is not needed.
window[this.name] = this;
is equivalent and doesn't use eval.

The article writes:
---
Since the name of the desired variable is a string, we must use the
eval() function to evaluate the constructed statement.
---
That was never true. You could assign global variables through the
window object in Netscape 2.02, pretty much the first browser to have
Javascript at all (I just tested it!).
this.animate = animate;
this.step = step;
this.show = show;
this.hide = hide;
this.left = left;
this.top = top;
this.moveTo = moveTo;
this.slideBy = slideBy;
this.slideTo = slideTo;
this.circle = circle;
This would be much simpler with a prototype object, which was
supported in NS4 and IE4.
} What do you think about that? That function generates global
variables and uses eval.


I can't say about the generation of global variables, since I don't
know what they are used for. I doubt that they are vital in any way.
I would just drop them, and then make sure make my own variables
for the objects I create.

Right after explaining how they create the global variable, they
show the example:
anim1 = new animation("ball1");
Why then make a global variable called "ball1Var" to refer to the
same object?

.... ok, I have now read through this, and I can see where they
use the global variable. It is in this line:

this.timer = setInterval(this.name + ".step()", interval);

An alternative with local variables and closures is:

var currentAnimation = this;
this.timer = setInterval(function(){currentAnimation.step()},in terval);

It works in NS4, but not in IE4.

So, ok, to support IE4, you might, in some cases, need to create globally
accessible values. I would still prefer to make one global object pointing
to an object, and then store the rest in there. No need to pollute the
global namescape.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5
Lasse:

Thanks for all the help. If you are interested, the code I was working on
is discussed at
www.devenezia.com/javascript - NavigationBar

--
Richard A. DeVenezia
Jul 20 '05 #6

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

Similar topics

5
3173
by: TG | last post by:
This is more of a pain than I thought it would be. I need a simple code segment to determine whether a browser accepts cookies or not. When I pass variables between pages when cookies are turned...
7
16725
by: Fuming Wang | last post by:
Hi, I have several modules that need to access global variables among them. I can do that by import modules: module A: gA = 'old' module B: import A
2
2181
by: Jerry | last post by:
My "main" class is getting a bit long...Is it possble to split a class definition into several files and then import the pieces to get the whole definition? Jerry
4
24159
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
1
2614
by: Yaro | last post by:
Hello DB2/NT 8.1.6 In procedure (SQL) I declare GLOBAL TEMPORARY TABLE. Sometimes this procedure is called twice a session and error creating table occure. I know, I can create table on session...
12
1817
by: Zeppelin | last post by:
Hi, I have to port from asp a script that adds an ip to a list in plain text or an array. I need not using a database or filesystem, it has to be stored in memory in order that giving us best...
2
2450
by: www.MessageMazes.com | last post by:
Greetings, I'm experimenting with an ASP page that reads data from a file name that is passed to it as a parameter, as in this page, which works, because the "good" file exists. ...
9
8613
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
10
8016
by: Dieter Pelz | last post by:
Hallo, what is the best way to check the installation of mfc80 and vcrt sidebyside assemblies? Best Regards, Dieter Pelz
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7011
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5596
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4689
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3170
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
401
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.