JavaScrip: closure scope and other ideas

Interesting to know about the scope of variables  that:
  1. 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:

  2. 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:

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

Print | posted @ Friday, October 14, 2011 4:22 PM

Comments on this entry:

Gravatar # re: Javascrip: closure scope and other ideas
by Nicolò Carandini at 10/16/2011 9:51 AM

Interessante notare come il focus si stia spostando su javascript.
chissà perché... ;-)

Your comment:

Italic Underline Blockquote Hyperlink
Please add 3 and 6 and type the answer here: