Monday, August 10, 2009

In most of the javascript examples of setTimeout I've seen, I see a string for the first argument,

setTimeout("do some stuff", 100);

where the contains javascript to be interpreted. I prefer using a function for the first argument,

setTimeout(function() { do some stuff }, 100);

I would imagine that the function is more efficient, but perhaps the string is so commonly used that it's been super optimized by javascript implementations. I think the function is cleaner, and theoretically allows syntax checking. If there are parameters involved in building the string, there's the possibility of script injection attacks. The function has lexical scoping. I imagine a new scope is created for the interpreted string.

No comments:

Post a Comment