From
http://learn.jquery.com/javascript-101/scope/
I do not understand why the first one is better than the second?
- // Functions can see changes in variable values after the function is defined
- var myFunction = function() {
- var foo = "hello";
- var myFn = function() {
- console.log( foo );
- };
- foo = "world";
- return myFn;
- };
-
- var f = myFunction();
- f(); // "world"
- var myFunction1 = function() {
- var foo = "hello1";
- var myFn = function() {
- console.log( foo );
- };
- foo = "world1";
- return myFn();
- };
-
- myFunction1();
And how this is more readable
- var foo = function() {
-
- }
than this?