This article describes the usage of anonymous functions in JavaScript. Share it for your reference. The specific analysis is as follows:
summary:
This article explains the most basic and important thing about JavaScript - functions. The reason I wrote this article is because I asked it during the interview, which is also considered to be reviewing the past and learning the new.
Let’s take an example first. If you understand it, it means you have understood what this article is going to talk about.
Copy the code as follows: var f = (function() {
function f() {return 10;}
return f();
function f() {return 20;}
var f = 30;
})();
console.log(f);
In JavaScript advanced programming, functions are described in this way - they can encapsulate any number of statements and can be called and executed anywhere and at any time. I have introduced strict mode before, and strict mode has some limitations on functions:
① The function cannot be named eval or arguments
② The parameters cannot be named as eval or arguments
③ Two named parameters cannot be the same name
The above situation will cause syntax errors and the code will not be executed.
Function definition
Function definitions are divided into three types
1. Constructor
The code copy is as follows: var fun = new Funciton();
2. Ordinary definition
Copy the code as follows: function fun() {}
3. Functional definition
The code copy is as follows: var fun = function() {};
All three ways can define function fun.
parameter
Functions don't mind how many parameters are passed in, nor do they care about what data type the parameters passed in. Even if the function you define only receives two parameters, you may not necessarily pass two parameters when calling this function. One or three can be passed or even no parameters. The reason is that the parameters are represented internally by an array. In the function body, you can access the parameter array through the arguments object, for example
Copy the code as follows: function saysHi() {
alert("Hello " + arguments[0] + "," + arguments[1]);
}
By accessing the length property of the arguments object, we can know how many parameters there are. The length of the function will return the number of parameters of the function.
Note: All parameters pass values, and it is impossible to pass parameters through references.
Functions cannot be overloaded, they can only be rewrited.
If two functions with the same name are defined, the name belongs to the last defined function, for example:
The code copy is as follows:
function add(num) {
return num + 100;
}
function add(num) {
return num + 200;
}
var result = add(100) //300
Note: The function stops and exits immediately after executing the return statement.
Function types
Functions are divided into two types: a name function and anonymous function. For example, the following famous function
Copy the code as follows: function fun() {
}
If called, just fun() is needed.
Anonymous functions, as the name implies, do not have function names. For example
function() {}
Function calls are called through function names. How to call anonymous functions? One is to assign an anonymous function to a variable, allowing this variable to act as the function name. Another way is to use () to call it, for example, the following three methods
1. (function() {return;}());
2. (function() {return;})();
3. function() {return;}();
example:
The code copy is as follows:
(function(x, y) {
alert(x + y);
})(2,3);
//alert(5)
2 and 3 will be passed as parameters to x and y
Let’s talk about the top example. This example involves closures, which will be discussed later.
First, define a variable f, and then assign an anonymous function. Here we need to note that the definitions of all variables in the function will be preset, so the execution order in the anonymous function is
The code copy is as follows:
var f = (function() {
var f = 30;
function f() {return 10;}
function f() {return 20;}
return f();
})();
The outer variable f and the inner variable f are not in the same scope (closure), so they do not affect each other. Because the function cannot be overloaded, the outer variable f=(function f() {return 20;})();, so the final output is 20.
I hope this article will be helpful to everyone's JavaScript programming.