What is a closure?
A closure is a feature in JavaScript where an inner function has access to the outer (enclosing) function’s variables.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function outer() { | |
let a = 10; | |
function inner() { | |
let b = 20; | |
console.log(a+b); | |
} | |
return inner; | |
} |
I think, that closures are very handy for recursive call.
What is a recursion?
Simply said, the process in which a function calls itself.When you create a recursive function, keep in mind that there must be an escape from the recursive function, otherwise you will get into an infinite loop.
Below I created a function, which call yourself. Recursion can be very complex, but the principle is the same.
Try it yourself:
No comments:
Post a Comment