<body onload= "alert (' http://www.baidu.com/');" ><script type= "Text/javascript" >voidfunction() { alert (' popup box ');} (); </script></body>
<body onload= "alert (' http://www.baidu.com/');" > <script type= "Text/javascript" > voidfunction() { alert (' popup ' </script></body>
<body onload= "alert (' http://www.baidu.com/');" ><script type= "Text/javascript" >(function() { alert (' popup box ');}) (); </script></body>
<body onload= "alert (' http://www.baidu.com/');" ><script type= "Text/javascript" >(function() { alert (' popup box ');}) (); </script></body>
As can be seen from the above example, the execution of the JS anonymous function is before the onload, then the empty function can replace the onload action?
JS anonymous functions are also written in more than N. As the name implies, an anonymous function is a function without a real name. For example, we remove the name of the function from the example above, and then judge if he is a function:
alert (typeof function () {}); // alert (typeof Span style= "color: #0000ff;" >function (x, y) {return x+y;}); // alert (typeof new Function ("x", "Y", "return x*y;")) // alert (typeof function () {}); //
It is easy to see that they are all function objects, in other words, they are functions, but they all have a feature--No name. So we call them "anonymous functions". However, because they do not have a "name", we have no way to find them. This extends the question of how to invoke an anonymous function.
Invocation of an anonymous function: To invoke a function, we must have a method to locate it and reference it. So, we'll need to find a name for it. For example:
var abc=function(x, y ) {return x+y;} alert (ABC (// var abc=functionreturn//
The above operation is tantamount to a different way to define the function, this usage is more frequently encountered by us. For example, when we set a DOM element event handler, we usually don't give them a name, but instead give it a corresponding event to reference an anonymous function.
The invocation of an anonymous function is actually another way of looking at the jquery fragment-using () enclosing the anonymous function, followed by a pair of parentheses (including the parameter list). Let's look at the following example:
Alert ((function(x, y) {return x+y;}) (2,3)); // Alert ((new Function ("x", "Y", "return x*y;")) (2,3)); // alert ((function(x, y) {return x+y;}) (2,3)); //
Many people may wonder why this method can be successfully invoked. Think the application of strange people look at me the following paragraph of the explanation.
Do you know the effect of parentheses? Parentheses can combine our expressions and each block, that is, each pair of parentheses, has a return value. This return value is actually the return value of the expression in parentheses. So, when we enclose the anonymous function with a pair of parentheses, the parentheses are actually returned, which is the function object of one of the anonymous functions. Therefore, the parenthesis pair plus the anonymous function is like the name of the function that we get its reference position. So if you add a parameter list after the reference variable, the invocation form of the normal function is implemented.
Do not know the above text expression can you see understand, if still can not understand, then look at the following code to try it.
var abc=function(x, y) {return x+y;}; // //alert ((ABC). constructor== (function(x, y) {return
Ps:constructor refers to the function that creates an object. This is the function body represented by the function object.
In summary, it is understood as the function object returned by the parenthesis expression, which is the anonymous function enclosed by parentheses, and can then be called as a normal argument list for this function object. (Here is a mistake, only the function expression can not directly call the function, the removal of the anonymous function parentheses must accompany the expression to assign value.) That is, (function () {alert (1)}) should be equivalent to A=function () {alert (1)} () and not even a=. )
JS anonymous function is really a good thing