1

Possible Duplicate:
How do JavaScript closures work?

function add(num){
    return function(num1){                
          return function(num2){                    
               return num + num1 + num2;
         };
    };
}

var add5 = add(7)((7))((7));
console.log(add5);

I've experimented quite a bit with JavaScript because I'm trying to grasp how closures work.

But then I've tried this

var add5 = add(7)((7))((7));
console.log(add5); //result is 21

The way I see it, are closures a kind of pointers to the outer function or its parent function?

Community
  • 1
  • 1
christian
  • 25
  • 1
  • 6

1 Answers1

2

Closure is scope of outer function, to which inner functions have acces, even if outer function execution ends. How exactly it is implemented, is up to JS interpreter.

Here are docs from MDN: https://developer.mozilla.org/en/JavaScript/Guide/Closures

Adam Jurczyk
  • 2,153
  • 12
  • 16