Guest

Untitled

By: a guest on Jan 28th, 2012  |  syntax: None  |  size: 0.50 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. // define a function
  2.  
  3. function foo() {
  4.   var bar = 1;
  5. };
  6.  
  7. // or
  8.  
  9. var foo = function() {
  10.   var bar = 1;
  11. };
  12.  
  13. // invoke foo
  14.  
  15. foo();
  16.  
  17. // if:
  18. //   foo === function() { var bar = 1; }
  19. //
  20. // then:
  21. //   foo() is equivalent to (function() { var bar = 1; })();
  22.  
  23. // why do this:
  24.  
  25. (function() {
  26.   var bar = 1;
  27. })();
  28.  
  29. // instead of this?
  30.  
  31. var foo = function() {
  32.   var bar = 1;
  33. };
  34.  
  35. foo();
  36.  
  37. // because the latter leaves a reference to foo in the
  38. // local scope, which you may not need