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