[whatwg] Scripting Tweaks

Dean Edwards dean at edwards.name
Wed Apr 20 11:27:18 PDT 2005


Brad Fults wrote:
> On 4/20/05, Dean Edwards <dean at edwards.name> wrote:
>>Yes, but as I said initially, that creates a closure. This is not always
>>the most efficient solution. Your code won't work anyway because "i" is
>>variable. The closure would need to be more complicated to work properly.
> 
> Talking about eval() and "efficient" should set off sirens in any JS
> developer's mind. Using eval() requires re-compilation of the code at
> runtime and is very rarely ever a real solution.
> 
> In addition, the proper argument to the setTimeout() function is a
> function reference, not a string. If you have a basic understanding of
> closures, they're not all that scary. Observe:
> 
> function fnMakeEnabled(oEl)
> {
>   return function() { oEl.disabled = false; };
> }
> ...
> for (...)
> {
>   elem[i].disabled = true;
>   setTimeout(fnMakeEnabled(elem[i]), 1);
> }
> 

The issue is not about closures it is about the ability to represent an 
element as a unique string. This can be useful for all sorts of reasons.

The example "non-scary" closure you supplied is too difficult for many 
programmers. Even Ian got it wrong at first and he is far from being a 
beginner. But, as I say, this is not the real issue.

Speaking of setTimeout, where is this defined?

-dean




More information about the whatwg mailing list