What does this syntax mean (function(){})()
as follows:
Quote
How to explain this
Everyone should remember this way of writing
var a=function (){}
Then how to run a
Then it is a()
The same principle
We don’t store it through the variable a, so how to write it
It is
function(){}()
But you will find that this is wrong
Because when the parsing engine parses, When parsing, I found that it was judged that the function ended
and the function was not run as a block
. Then adding () forced the function block to be used as a block
by flashsoft
Reference
should be called function literal.
function f(x) {return x*x;}
var f=function(x) {return x*x;} ----The function defined in this way is called a function literal , is a way to define functions. It is an expression rather than a statement. This function is an anonymous function, f is just a reference to the function, not the function name.
Function literals can be stored in a variable or passed to other functions or even "called directly".
var t=(function(x) {return x*x;})(10);//Define a function and call this function.
--------The above quote is from "The Definitive Guide to JavaScript" by lyxscn
Quote <script> <BR>(function(){ <BR>document.write("some script code"); <BR>})() <BR></script> just like java's anonymous class , define and execute a function <script> <BR><!-- <BR>var a=(function(s){return s})("abc"); <BR>alert(a); <BR>var b=function(s){return s}; <BR>alert(b("abc")); <BR>//--> <BR></script>