JavaScript function parameters
The 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 a JavaScript function is defined, the display 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 no implicit parameters are provided when the function is called, the parameters will be set to: undefined
Sometimes this is Acceptable, but it is recommended to set a default value for the parameter:
Instance
<!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 instance» Click the "Run Instance" button to view the online instance
Or, easier way:
Instance
<!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 instance»Click the "Run instance" button to view the online instance
| ##If y has been defined , y || returns y, because y is true, otherwise 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:
Example
<!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>
Running example »Click the "Run Example" button to view the online example
Or create a function to count the sum of all values:
Example
<!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 Instance»Click the "Run Instance" button to view the online instance
Pass parameters by value
The parameters called in the function are implicit parameters of the function.
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 the value of an object.
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.