Working with JS may be difficult for a beginner. Or for someone like me, who dares to think of himself as a … let’s call it not a beginner. 🙂
One of the strangest staff there, if you are coming from the VBA world (like me) is the option of a function to return a function, returning a function (and you may return functions in functions as much as you want).
Let’s see how this happens. In the sample, we create a function “outer”, returning a function, returning another one. Each one of the functions writes on the console, so you may see how deep have we gone inside. The trick in going deeper into the functions is to use the parenthesis.
This is the result:
Here comes the code, enjoy it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<script> function outer(){ console.log("vitosh"); return function(){ console.log("academy"); return function(){ console.log(".com"); } } } console.log("Now with three:"); outer()()(); console.log("Now with two:"); outer()(); console.log("Now with one:"); outer(); </script> |
🙂