I. JavaScript functions
Function: a function is an encapsulation, event driven, or reusable block of code that executes when it is called.
Define function:
Function function name (){
Function body;
}
The number will not be automatically executed. It needs to be called before it can be executed
Function name ();
Function naming rules:
Consistent with variable naming rules
1) start with letters,,, 2) can contain numbers, letters,,, 2) can contain numbers, letters
open
The first 2) can contain numbers, letters_
3) case sensitive
4) cannot use keywords and reserved words
Hump nomenclature: if the name consists of more than one word, start with the second word and capitalize the first letter
Distinguish variable name and function name
Unwritten rules:
Function name verb
Variable NOUN
The order in which functions are defined is independent of the order in which they are called
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script type="text/javascript"> //Output 5 lines of hello world on the page //Defined function function printHello(){ for(var i=0;i<5;i++){ document.write('hello world<br>'); } } printHello(); function call </script> </body> </html>
2. Function parameter call;
Parameters: parameters in function definition
Arguments: arguments for function consumption
Theoretically, a function can have an infinite number of parameters separated by commas
Unlimited function parameter types
Case 1 /Print graphics * *** ***** ******* ********* ... <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script type="text/javascript"> function show(row){ for(i=1;i<=row;i++){ for(j=1;j<=2*i-1;j++){ document.write('*'); }document.write('<br>'); } } show(5); </script> </body> </html>
Case 2 //Calculate the area of a rectangle <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script type="text/javascript"> function calRet(w,h){ var w; var h; var area=w*h; console.log(area); } calRet(20,10); </script> </body> </html>
Case 3: multiple function parameters <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script type="text/javascript"> function person(name,age,gender){ var man='Sir'; if (!gender) { man='Ma'am'; }document.write('Welcome'+name+man+'Visit to school,Age:'+age+'<br>'); } person('Cockroach',60,true); person('Cockroach',60,false); </script> </body> </html>
Case 4: time call <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script type="text/javascript"> var o=document.getElementById('box'); function showTime(){ //Get year, month, day var date = new Date; var year=date.getFullYear(); var month=date.getMonth()+1; var day=date.getDate(); //Get hour, minute, second var hour=date.getHours(); var minute=date.getMinutes(); var second=date.getSeconds(); hour=add(hour); minute=add(minute); second=add(second); time=year+'/'+month+'/'+day+' '+hour+':'+minute+':'+second; o.innerHTML=time; } function add(num){ if (num<10) { num='0'+num; } return num; } showTime(); setInterval(showTime,1000); </script> </body> </html>