Pages

Monday, August 29, 2016

Recursive reflections

If I call an inner function recursively, will it remember the setting of variables in an outer function?

The answer is yes!

For example:

var tester = function(a){
b = 0;
function poop(){
if(a === b){
return a;
}
console.log(b);
b++;
poop();

}
poop();
}

var maple = tester(5);
0
1
2
3
4

will print to the console.  I suppose this is a recursive function that produces only side effects and returns nothing.

No comments:

Post a Comment