Javascript function experience

Javascript functions

Javascript functions are event driven or reusable blocks of code that execute when called. So it's useless for us to create a function without calling it. We have to call it to execute it.

1. What is a function

(1) a function is a code segment that performs a function

(2) functions are repeatable code segments

(3) convenient function maintenance and management

2. What should we pay attention to when creating functions?

(1) function names are strictly case sensitive

(2) duplicate function names will result in overwriting

(3) function name is best semantically

3. Trigger function

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title></title>
 6 </head>
 7 <body>
 8     <button onclick="Click()">Click the button to call the function</button>
 9     <script>
10         function Click(){
11             alert('Call succeeded!');
12         }
13     </script>
14 </body>
15 </html>

4. Functions to implement default parameters

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title></title>
 6 </head>
 7 <body>
 8     <script>
 9     function calc(x,y){
10         x=x||0;
11         y=y||0;
12         return x+y;
13     }
14       alert(calc());
15       alert(calc(1,2));
16     </script>
17 </body>
18 </html>

5. Variable parameter function

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title></title>
 6 </head>
 7 <body>
 8     <script>
 9         function test(){
10             var sum = 0 ;
11             var allNum = arguments.length ; //Definition allNum Is the number of parameters passed in
12             for(var i=0;i<allNum;i++){
13                 sum+=arguments[i];//sum = sum + arguments[i]
14                 document.write(arguments[i]);
15             }
16             return sum;
17         }
18         alert(test(1,2,4));
19     </script>
20 </body>
21 </html>

6. Variable scope

(1) local variable: the variable declared in the function body, which can only be used in the function body

(2) global variables: global variables, which can be used from declaring variables to the end of the script

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title></title>
 6 </head>
 7 <body>
 8     <script>
 9         var x=1;
10         function test(){
11             document.write('1.In function body x The value of is:'+x+'<br />');
12             x=19;
13             document.write('2.In this case, the function body is re aligned x Value assignment, at this time x The value of is:'+x+'<br />');
14         }
15         document.write('3.Out of function x Value:'+x+'<br />');//Because the read-write sequence first executes the code and then executes the function test()
16         test();
17         document.write('4.Out of function x Value:'+x+'<br />');
18     </script>
19 </body>
20 </html>

This is just my personal learning experience, where there are shortcomings, please put forward. Thank you for your advice!!

Keywords: Javascript

Added by sanand158 on Sat, 02 May 2020 03:18:49 +0300