Interesting to know about the scope of variables that:
- In Javascript there is no block scope, it means that a variable declared in a block (as a for body or an if body) belong to the containing function scope.
In this example you see that all function created and stored in functs[] indeed refer to the same value of sWithBlockScope: http://www.pastie.org/2694945
- Here all the function created and stored in functs[] are created inside a function scope from the function makeFunction so each one have its own different value for sWithBlockScope: http://www.pastie.org/2694947
Other interesting things to look at are:
- Currying in Javascript (a way to create a new function from a given one setting a constant value for a parameter)
- Memoization in Javascript (a way to cache function calls that are repeated multiple times)
- Mixin in Javascript (another way to reuse functions)
Update: throwing and catching exceptions in Javascript, some code sasmple
http://www.pastie.org/2723772