Summary of some important points of JavaScript functions and js anonymous functions

  • 2020-10-23 20:00:28
  • OfStack

Let me introduce you to the javascript function

The basic syntax of the function is:


function functionName(arg0,arg1,...,argN) {
 statements
}

Here is an example:


function str(name,age){
 document.write("hello my name is " + name + ". and i am " + age + " years old.");
}
str(" oliver",23); //hello my name is oliver. and i am 23 years old.

In addition, any function can return a value at any time by following the return statement with the value to be returned. Such as:


function sum(num1,num2){
 return num1 + num2;
 alert("hello"); // return return  It will not continue after that alert
}
var result = sum(321,32);
document.write(result); //353

Because the return statement is stopped and exits immediately after it is executed, no code after the return statement is executed.

Of course, one function can contain more than one return statement. Such as:


function conp(a,b){
 if (a > b){
 return a;
 }else if (a == b){
 return "equal";
 }else{
 return b;
 }
}
var result = conp(4,4);
document.write(result); //equal
var result = conp(321,4);
document.write(result); //321

In addition, the return statement can also have no return value. This way, you can immediately stop function execution and return undefined. Such as:


function conp(a,b){
 if (a > b){
 return;
 document.write("bad");
 }else{
 document.write(b);
 }
}
var a = conp(33,3);
document.write(a); // return undefined  And it's not gonna happen "bad"

Parameters of a function

The ECMAScript function can take any number of arguments, or any data type. It can be accessed by the arguments object in the body of the function, for example, the first parameter is arguments[0], the second is arguments[1], and so on. Named parameters are a convenience, not a requirement. Such as:


function greeting(){
 document.write("hello " + arguments[0] + ". you look " + arguments[1] + ".");
}
greeting("oliver","good"); //hello oliver. you look good.

In addition, you can access the length property of the arguments object to see how many arguments are passed to the function. Such as:


function countArguments(){
 document.write("there are " + arguments.length + " arguments here.");
}
countArguments(321,321,32,32); //there are 4 arguments here.

You can use this point in combination with the if statement to make a judgment. Such as:


function count(){
 if (arguments.length == 1){
 document.write("you just have 1 arguments.");
 }else{
 document.write("you have many arguments.");
 }
}
count(321,321,321) //you have many arguments.

In addition, arguments[] can be used with named parameter 1.

Function overloading (no overloading)

If two arguments with the same name are defined, the name change belongs only to the function defined later. Such as:


function add(){
 document.write(arguments[0] + arguments[1]);
}
function add(){
 document.write(arguments[0] + 100);
}
add(321,2); //421  No regulation will be enforced 1 A function (adding two arguments) that executes only at the end 1 The function of the same name (the 1 Number of parameters plus 100 ) 

PS:JavaScript anonymous function

Functions are one of the most flexible objects in JavaScript, but the purpose of their anonymous functions is explained here. Anonymous functions: Functions that do not have a function name.

1.1 Definition of functions: Firstly, the definition of functions under 1 is briefly introduced, which can be roughly divided into three ways

Type 1: This is also the most common type


function double(x){
 return 2 * x; 
}

Option 2: This method USES the Function constructor and takes both the argument list and the body of the function as strings, which is inconvenient and not recommended.


var double = new Function('x', 'return 2 * x;');

Third:


var double = function(x) { return 2* x; }

Note that the function to the right of "=" is an anonymous function that is assigned to the variable square after the function is created.

1.2 Creation of anonymous functions

The first way is to define the square function as described above, which is also one of the most common ways.

The second way:


function str(name,age){
 document.write("hello my name is " + name + ". and i am " + age + " years old.");
}
str(" oliver",23); //hello my name is oliver. and i am 23 years old.
0

Here, an anonymous function is created (within the first parenthesis), and the second parenthesis is used to call the anonymous function and pass in parameters.


Related articles: