JavaScript func...LOGIN

JavaScript function parameters

JavaScript function does not perform any check on the value of the parameter.


Function explicit parameters (Parameters) and implicit parameters (Arguments)

In the previous tutorial, We have learned about the explicit parameters of the function:

functionName(parameter1, parameter2, parameter3) {
// The code to be executed...
}

Explicit parameters of functions are listed when the function is defined.

Implicit function parameters are the real values ​​passed to the function when the function is called.


Parameter rules

When the JavaScript function is defined, the displayed parameter does not specify a data type.

JavaScript functions do not perform type detection on implicit parameters.

The JavaScript function does not detect the number of implicit parameters.


Default parameters

If the function is not called when called Provide implicit parameters, and the parameters will be set by default to: undefined

Sometimes this is acceptable, but it is recommended to set a default value for the parameter:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>php中文网(php.cn)</title>
</head>
<body>
<p>设置参数的默认值。</p>
<p id="demo"></p>
<script>
    function myFunction(x, y) {
        if (y === undefined) {
            y = 0;
        }
        return x * y;
    }
    document.getElementById("demo").innerHTML = myFunction(4);
</script>
</body>
</html>

Run the program and try it


Or, a simpler way:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<p>设置函数参数默认值。</p>
<p id="demo"></p>
<script>
function myFunction(x, y) {
    y = y || 0;
    return x * y;
}
document.getElementById("demo").innerHTML = myFunction(4);
</script>
</body>
</html>

Run the program and try it

Tip: If y has been defined, y || returns y, because y is true, otherwise it returns 0, because undefined is false.


If too many parameters are set when the function is called, the parameters will not be referenced because the corresponding parameter names cannot be found. Can only be called using the arguments object.

Arguments object

JavaScript function has a built-in object arguments object.

The argument object contains the parameter array of the function call.

In this way you can easily find the value of the last parameter:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<p>查找最大的数。</p>
<p id="demo"></p>
<script>
function findMax() {
    var i, max = 0;
    for(i = 0; i < arguments.length; i++) {
if (arguments[i] > max) {
            max = arguments[i];
        }
    }
    return max;
} 
document.getElementById("demo").innerHTML = findMax(4, 5, 6);
</script>
</body>
</html>

Run the program to try it

Or create a function to count the sum of all values :

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<p>计算所有参数之和:</p>
<p id="demo"></p>
<script>
function sumAll() {
    var i, sum = 0;
    for(i = 0; i < arguments.length; i++) {
        sum += arguments[i];
    }
    return sum;
} 
document.getElementById("demo").innerHTML =
sumAll(1, 123, 500, 115, 44, 88);
</script>
</body>
</html>

Run the program to try it


Pass parameters by value

The parameters called in the function are Implicit parameters of functions.

JavaScript implicit parameters are passed by value: the function just gets the value.

If the function modifies the value of the parameter, the initial value of the explicit parameter (defined outside the function) will not be modified.

Changes to implicit parameters are not visible outside the function.


Passing parameters through objects

In JavaScript, you can reference objects value.

So when we modify the properties of the object inside the function, its initial value will be modified.

Modifying object properties can act outside the function (global variables).

Modifying object properties is visible outside the function.



Next Section
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p>查找最大的数。</p> <p id="demo"></p> <script> function findMax() { var i, max = 0; for(i = 0; i < arguments.length; i++) { if (arguments[i] > max) { max = arguments[i]; } } return max; } document.getElementById("demo").innerHTML = findMax(4, 5, 6); </script> </body> </html>
submitReset Code
ChapterCourseware