Q: Check existence of several global variables

  • Thread starter Richard A. DeVenezia
  • Start date
R

Richard A. DeVenezia

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 )
 
L

Lasse Reichstein Nielsen

Richard A. DeVenezia said:
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
 
V

Vjekoslav Begovic

and later...
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.
 
R

Richard Cornford

Vjekoslav Begovic said:
and later...


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;
eval(this.name + " = this");

window[this.name] = this; //same effect, no eval, and faster!

}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.
 
L

Lasse Reichstein Nielsen

Vjekoslav Begovic said:
and later...

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()},interval);

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
 

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

Forum statistics

Threads
474,083
Messages
2,570,591
Members
47,212
Latest member
RobynWiley

Latest Threads

Top