Function
-
- 1. Definition and calling of functions
- 2. Anonymous functions
1. Definition and calling of functions
1. Function: also known as “method”, solves the problem: realizes the encapsulation of code blocks corresponding to different functions
?Example: Function/method—–>Encapsulated code block—–>Realize a certain function
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <script language="javascript"> //Fun01 with no parameters and no return value function fun01(){ alert("This is a button!"); } fun01(); </script> </body> </html>
2.Declaration/Create function
function function name (Parameter name 1, Parameter name 2…){
? Function body;—->The code block corresponding to the function to be implemented by the function
return return value;
}
Remarks: ① The parameters within parentheses are writable;
? ②The return statement can be written. If the function needs to have a return value, associate it;
? ③The parameters set when declaring the function are called == “formal parameters” , referred to as “formal parameters”==;
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <script language="javascript"> //Function fun02 with parameters and no return value function fun02(x,y,z){ alert(x-y-z); } fun02(100,40,35); </script> </body> </html>
3.Function call: Run/execute the created function
Format: function name(); or function name(actual parameters);
Remark:
? Actual parameters: Actual parameters, which refers to the assignment operation performed when calling a function with formal parameters;
? Actual parameter is the value assigned to the formal parameter: The actual parameter is passed to the formal parameter
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <script language="javascript"> //Function fun03 with no parameters and return value function fun03(){ return(Math.random()*100); } var f=fun03(); alert(f) </script> </body> </html>
4.return statement:
There is no output function, it can only return data. If you want to display the returned result value, you have to use an output statement;
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <script language="javascript"> //Function fun04 with parameters and return value function fun04(i,j,k){ alert("The multiplication of three numbers is:"); return(i*j*k); } //var ijk=fun04(Math.random(),Math.random(),Math.random()); //alert(ijk); var ijk=fun04(fun03(),fun03(),fun03()); alert(ijk); </script> </body> </html>
2. Anonymous functions
1.Overview: Functions have no names and are used in combination with variables
2.Format:
var variable name=function(Parameter name){
? Function body;
? return return value;
}
3. Call: variable name(); variable name(value);
Example:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Anonymous function</title> \t\t <script language="JavaScript"> \t\t\t //Declare an anonymous function: implement the remainder operation of two numbers //Anonymous function with parameters and return value var fun01=function(x,y){//x=10 y=24 \t\t\t\t //Return the remainder result return x%y; //63%9=0 } \t\t\t//transfer alert(fun01(10,24)); \t\t\t alert(fun01(11,10)); \t\t\t //Declare an anonymous function with no parameters and no return value var fun02=function(){ //Output alert("I am an anonymous function with no parameters and no return value"); } \t\t\t//transfer fun02(); </script> </head> <body> </body> </html>
4. Global functions: functions that can be used directly;
1> eval(): Identify and execute the JS code contained in the string;
2>Chinese encoding: encodeURI()
Example:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Encoding</title> </head> <body> <script language="javascript"> //Me and my motherland var name=prompt("Please enter..."); var bbb=encodeURI(name); if(bbb=="My motherland and I"){ alert("Verified correctly!"); }else{ alert("Big idiot!"); } </script> </body> </html>
3> Chinese decoding: decodeURI()
Example:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>js</title> \t\t <script language="JavaScript"> //③Chinese decoding: decodeURI() var i="A loving family"; //Decode the value of i var jm=decodeURI(i); //Output A family that loves each other console.log(jm); \t\t\t </script> \t\t </head> <body> </body> </html>
4> isNaN(): Determine whether it is a number, is a number returns false, is not a number returns true: see Literal value
Example:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>js</title> \t\t <script language="JavaScript"> //isNaN(): Determine whether it is a number. If it is a number, it returns false. If it is not a number, it returns true: look at the literal value. var x="testing"; var y=26; var z="69"; console.log(isNaN(x)); //true console.log(isNaN(y)); //false console.log(isNaN(z)); //false </script> \t\t </head> <body> </body> </html>
5> parselnt(): Convert the numbers in the string to the original data type;
Example:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>js</title> \t\t <script language="JavaScript"> \t\t\t //parseInt(): Convert the numbers in the string to the original data type var s="123"; alert(s + 1); //1231 alert(parseInt(s) + 1); //124 </script> </head> <body> </body> </html>