function
1. Introduction to functions
Function is also an object, which can encapsulate some functions (code), execute these functions (code) when needed, and save some code to call when needed
When you use typeof to check a function object, function is returned
// Create a function object // You can pass the code to be encapsulated to the constructor as a string var fun = new Function("console.log('Hello World.');"); // Code encapsulated in a function does not execute immediately // The code in the function will be executed when the function is called // Calling function syntax: function object () // When a function is called, the code encapsulated in the function is executed in order fun(); // Hello World.
Create a function using a function declaration
function Function name([Formal parameter 1, Formal parameter 2...Formal parameter N]) { sentence... } // Call function Function name();
Example
function fun1(){ console.log("Hello world."); alert("Hello World!"); document.write("Helloworld"); } fun1();
Use a function expression (anonymous function) to create a function
var Function name = function([Formal parameter 1, Formal parameter 2...Formal parameter N]) { sentence... }; // Call function Function name();
Example
var fun1 = function(){ console.log("Hello world."); alert("Hello World!"); document.write("Helloworld"); }; fun1();
2. Parameters of function
Define a function to find the sum of two numbers
One or more formal parameters (formal parameters) can be specified in () of the function. Multiple formal parameters are separated. Declaring formal parameters is equivalent to declaring corresponding variables inside the function
When calling a function, you can specify arguments (actual parameters) in ()
-
When a function is called, the parser does not check the type of the argument. Therefore, you should pay attention to whether illegal parameters may be received. If possible, you need to check the type of parameters
-
When calling a function, the parser does not check the number of arguments, and redundant arguments are not assigned. If the number of arguments is less than the number of formal parameters, formal parameters without corresponding arguments will be undefined
// Create a function to calculate the sum of three numbers function sum(a, b, c) { alert(a + b + c); } sum(1, 2, 3, 4); // 6
3. Return value of function
You can use return to set the return value of a function. Syntax: return value
The value after return will be returned as the execution result of the function. You can define a variable to receive the result
In the function, the statements after return will not be executed
If the return statement is not followed by any value, it is equivalent to returning an undefined; If return is not written in the function, undefined will also be returned
return can be followed by any type of value
// Create a function to calculate the sum of three numbers function sum(a, b, c) { // var result = a + b + c; // return result; return a + b + c; } // Call function // The value of the variable result is the execution result of the function // The function returns the value of result var result = sum(1, 2, 3); console.log("result = " + result);
practice
// 1. Define a function to judge whether a number is even. If yes, it returns true; otherwise, it returns false function isEven(num){ // if(num % 2 == 0){ // return true; // } // return false; return num % 2 == 0; } var result = isEven(6); console.log(result); // true result = isEven(7); console.log(result); // false // 2. Define a function that can calculate the area of a circle according to the radius and return the calculation result function calCirc(radius) { return 3.14 * Math.square(radius); } var result = calCirc(2); //
The argument can be any data type or an object. When we have too many parameters, we can encapsulate the parameters into an object
function sayHello(o){ console.log("I am" + o.name + ",This year I" + o.age + "Years old, I am a" + o.gender + "People, I live in" + o.address); } var obj = { name: "Sun WuKong", age: 1000, gender: "male", address: "Huaguo Mountain" }; sayHello(obj); // I'm Monkey King. I'm 1000 years old this year. I'm a man. I live in Huaguo Mountain
An argument can be an object or a function
function calCirc(radius) { return Math.PI * Math.pow(radius, 2); } function fun(a){ console.log("a = " + a); } fun(calCirc); // a = function calCirc(radius) { // return Math.PI * Math.pow(radius, 2); // } fun(calCirc(10)); // a = 314.1592653589793
calCirc(10)
- Call function
- Equivalent to the return value of the function used
calCirc
- Function object
- It is equivalent to using function objects directly
Function is also an object, especially in its function
Comparison of break, continue and return
- break can exit the current loop
- continue is used to skip the current cycle
- return can end the entire function
Declare another function inside the function
function fun3(){ function fun4(){ console.log("I'm fun4."); } fun4(); } fun3(); // I'm fun4. function fun5(){ function fun6(){ console.log("I'm fun6."); } return fun6; } var a = fun5(); a(); // I'm fun6. fun5()(); // I'm fun6.
4. Execute function now
After the function is defined, it is called immediately. This function is called immediate execution function
Functions that execute immediately are often executed only once
// Function object () (function(){ console.log("I'm anoymous function."); })(); // I'm anoymous function. (function(a, b){ console.log(a + b); })(2,3); // 5