Experts take a detour! This has nothing to do with the closure itself. I don’t know how to choose the title. I just made up the number. I hope you can forgive me!
Today a friend who just learned js gave me a piece of code to ask why the method is not executed. The code is as follows:
function makefunc(x) {
return function ( ; It's "0", not function (){return x;}.
It’s not that the script was written wrong, it’s just that I didn’t understand return. Exit from the current function and return a value from that function. If a function is returned, then the function itself is returned.
You can modify the above code like this, which is alert(makefunc(0)()):
Copy the code
The code is as follows:
alert(makefunc(0 )());
If you want to return the result of function execution, you must first let the function execute, for example:
Copy code
The code is as follows:
alert(makefunc(0) );
There is an anonymous function here,
Copy the code
The code is as follows:
is an anonymous function in the first bracket, and the second bracket is used to call This anonymous function, you can pass in the required parameters in the second bracket. For example:
Copy code
The code is as follows: