čtvrtek 13. ledna 2011

javascript closures in ECMA script standard

When using something I have a strong need to understand things as deep as possible. I was struggling advanced javascript code using closures heavily. Almost every explanation I found was "example based". As I really need to know the internals to understand the problem I decided to dive into ECMA script specification to find parts related to closures. At first I searched ECMA-262 second edition published back in the last century (1998). Well ... It is really a history. Nothing related to or similar to closures could be found in this document. The I have tried the fifth edition published in 2009. BINGO!

No explanation of closures will follow in this post. I think the description in the spec is crystal clear and should be read by everyone who wants to understand what's going on when a nested function is used.

The shortcut for those who are curious is here: look for Clause 10.2 in the ECMAScript specification document (http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf). Its name is "Lexical enviroments". This clause describes how the javascript engine implements "the magic". Clause 13.2 is otyher usefull part describing function object instantiation.