JavaScript func...LOGIN

JavaScript function preliminary

The concept of function

  • #A function encapsulates a piece of public code and gives it a name "function" .

  • A function can be defined once and called multiple times.

  • function can encapsulate commonly used function codes. Such as: user name verification, verification code function, email verification, mobile phone number verification

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script>
            function max(a,b){
                if(a>b){
                    document.write(a+"比较大<br/>");
                }else{
                    document.write(b+"比较大<br/>");
                }
            }
            max(10,50);
            max(0,100);
            max(-1,2);
            max(100,100);
        </script>
    </head>
    <body>
    </body>
</html>

function definition format

function functionName([参数1][,参数2][,参数N]){
    函数的功能代码;
    [return 参数r]
}

Explanation of function definition format

  • The function keyword is required, all lowercase.

  • functionName: The name of the function. The naming rules for function names are the same as for variable naming.

  • (): It is the parameter that receives data when defining the function. Parameters are optional, and multiple parameters are separated by commas.

  • Formal parameters (formal parameters): The parameters when defining a function are "formal parameters". Mainly used to receive data passed by the caller of the function.

  • #The name of the formal parameter is the same as the naming rule of the variable.

  • But formal parameters cannot be defined with the "var" keyword.

  • Parameters can only exist in functions.

  • Parameters do not need to be declared and can be used directly.

  • Actual parameters (actual parameters): The parameters when calling a function are called "actual parameters". Actual parameters are real data.

  • #{} is the function of the function.

  • The return statement is used to return a value to the function caller and immediately end the function.

  • #return is used to abort the running of the function.

  • break is used to terminate various loops.


Function call

The function definition will not be executed, then the function Must be called to have effect.

Function call: directly write the function name followed by parentheses (), and if there are parameters, write the parameters. Parentheses cannot be omitted.


Parameters of the function

  • The number of formal parameters must be the same as the number of actual parameters The number is the same;

  • The order of formal parameters must be consistent with the order of actual parameters.

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script>
        //定义函数
            function information(name,age){
               document.write("大家好,我叫"+name+",今年"+age+"岁<br/>")
            }
            information("张三",24);
            information("李四",30);
            information("涛哥",20);
        </script>
    </head>
    <body>
    </body>
</html>
Next Section
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> function max(a,b){ if(a>b){ document.write(a+"比较大<br/>"); }else{ document.write(b+"比较大<br/>"); } } max(10,50); max(0,100); max(-1,2); max(100,100); </script> </head> <body> </body> </html>
submitReset Code
ChapterCourseware