W3C home > Mailing lists > Public > whatwg@whatwg.org > April 2005

[whatwg] Scripting Tweaks

From: Dean Edwards <dean@edwards.name>
Date: Wed, 20 Apr 2005 19:27:18 +0100
Message-ID: <42669F06.7070600@edwards.name>
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
Received on Wednesday, 20 April 2005 11:27:18 UTC

This archive was generated by hypermail 2.3.1 : Monday, 13 April 2015 23:08:22 UTC