On Feb 20, 6:53 am, "David" <n...@none.netwrote:
"David Golightly" <davig...@gmail.comwrote >
For one thing, you shouldn't assign rogue function handlers in
function bodies, mostly because IE's garbage collector can't clean
them up:
http://blogs.msdn.com/ie/archive/200...performance-re...
but also because a new copy of the anonymous function is created for
each element, which is much less efficient than having all elements
sharing the same function:
You know, I don't think this would apply in this case. The outer function is
called only once onload to initiate the script, and the inner function. So
this function and the nested function objects ( applied to specific id'd
anchor tags ) are already stored in memory. I don't think extra instances of
these function objetcs are created when the event happens?
David
It's true that, if you only call this once, you'll only get one copy
of the function. But you're still getting into a potentially
problematic circular reference with your closure. Any time a function
exists has an element in its scope that it in turn is attached to (as
an event handler), you're creating exactly the kind of circular
reference referred to in the MSIE blog article - the one the IE team
decided wasn't worth fixing.
Also, were you to put this event handler assignment (doIt) in a loop,
you'd get multiple copies of the function literal assigned as
properties of your different DOM objects, instead of your DOM objects
having references to the same function object, since with each outer
function call you'll get a different scope chain and therefore a new
closure. See:
http://developer.mozilla.org/en/docs...considerations
Finally, it's probably not good programming practice to just say "Oh,
I'll just do it like this because it's a one-off thing, no need to
worry about scalability." If I can get the same result using a more
scalable technique, I get in the habit of doing it that way. You
never know when you may need to scale in the future! Just my 2c,
however; you're welcome to develop your own technique.
-David