Tuesday, August 9th, 2005

JavaScript Gotcha

Category: JavaScript

Keith ran into one of the classic JavaScript Gotchas wrt closures and sharing function objects resulting from:

Browser independant code *may not* rely on obtaining a different,
unjoined function object for *any* function declared inside another
function, whether this be because of function declaration or expression within a loop, across calls to the containing function, or whether the function be annonomous or named. ECMA 262 both *allows* them to be different in all cases and *allows* them to share all external properties (including prototype and user defined properties) in all cases (because of join operations retaining differences in the scope chain) and in some cases even *allows* them to be the same function object without joining.

Posted by Dion Almaer at 11:54 am
1 Comment

3 rating from 5 votes

1 Comment »

Comments feed

Keith was mistaken about his “gotcha”. It has nothing to do with joined or unjoined functions.

He was creating a bunch of functions within a single function invocation. All of the created functions shared the same scope. He wanted the functions to have independent copies of the local variables, but they all shared a single copy.

Keith has the correct workaround posted on his blog, he just had the diagnosis wrong.

Nobody panic about closures and joining! That matters only to people writing their own JavaScript interpreters.

Comment by David Flanagan — August 10, 2005

Leave a comment

You must be logged in to post a comment.