Home > Article > Web Front-end > Summary of some important points to note in JavaScript functions and js anonymous functions_javascript skills
Let me introduce to you the javascript function
The basic syntax of thefunction 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 using a return statement followed by the value to be returned. Such as:
function sum(num1,num2){ return num1 + num2; alert("hello"); //返回return 之后不会继续执行alert } var result = sum(321,32); document.write(result); //353
Because execution stops and exits immediately after the return statement, any code located after the return statement will not be executed.
Of course, a function can contain multiple return statements. 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 not have any return value. In this way, function execution can be stopped immediately and undefined returned. 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); //返回undefined 且不会出现"bad"
Parameters of function
ECMAScript function parameters can be any number and can be of any data type. It can be accessed through the arguments object in the function body, such as the first parameter is arguments[0], the second is arguments[1], and so on. Named parameters are a convenience but are not required. 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 get how many parameters are passed to the function by accessing the length property of the arguments object. 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 to make judgments in combination with if statements. 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.
Also, arguments[] can be used with named parameters.
Overloading of functions (no overloading)
If two parameters with the same name are defined, the name change only belongs 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 不会执行第一个函数(两个参数相加),只执行最后一个同名的函数(第一个参数加上100)
PS: JavaScript anonymous function
Function is the most flexible object in JavaScript. Here we only explain the use of its anonymous functions. Anonymous function: It is a function without a function name.
1.1 Definition of function, first briefly introduce the definition of function, which can be roughly divided into three ways
The first type: This is also the most common type
function double(x){ return 2 * x; }
Second method: This method uses the Function constructor and treats both the parameter list and the function body as strings. This is very inconvenient and is not recommended.
Third type:
Note that the function on the right side of "=" is an anonymous function. After creating the function, the function is assigned to the variable square.
1.2 Creation of anonymous functions
The first method is to define the square function as mentioned above, which is also one of the most commonly used methods.
The second way:
(function(x, y){ alert(x + y); })(2, 3);
An anonymous function is created here (inside the first bracket), and the second bracket is used to call the anonymous function and pass in the parameters.