Examples of calls to js anonymous functions come in a variety of forms

  • 2020-03-30 03:45:03
  • OfStack

An anonymous function is one that has no actual name.

JavaScript's anonymous functions come in a variety of forms and are confusing to read.

Here are the anonymous functions that were successfully called:


(function () {
alert(3);
})
(); (function f1() {
alert(4);
})(); //Not an anonymous function can be called like this!! void function(){
alert('void water');
}();//Said to be the most efficient, in Javascript void is an operator that specifies that an expression is to be evaluated but does not return a value. < br / >
!function(){
alert(' ! water');
}(); //Operator + anonymous function call (function(){
alert('water');
}());//Bracket + anonymous function, a bit of a bit of a force ~

There are also common mistakes to be aware of when using anonymous functions:

//Error 1
(function f1() {
alert(5);
})f1(); //This is not an anonymous function! //Error 2
(function () {
alert(6);
}); //There are no errors in the syntax, no anonymous functions to call, and no opportunities to call later, because there is no name, the call entry cannot be found. //Error 3
function () {
alert(1);
}();//Call
without generating a reference to the function

Also be careful to understand what the braces do.

The parentheses combine our expressions into blocks, and each block, or pair of braces, has a return value. This return value is essentially the return value of the expression in parentheses. So when we put a pair of parentheses around an anonymous Function, the pair of parentheses actually returns the Function object of an anonymous Function. So, the pair of braces with the anonymous function is just as much a reference as a function with a name. So if you add a list of arguments after the reference variable, you'll get a normal function call.


Related articles: