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 @ venerdì 14 ottobre 2011 19:22

Comments have been closed on this topic.