JavaScript function parameters
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) {
// Code to be executed...
}
Explicit parameters of the function 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 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 does not provide implicit parameters when calling, the parameters will be set to: undefined by default
Sometimes this is acceptable, but it is recommended to be The parameter sets a default value:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </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>
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:
Source code:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </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>
Or create a function to count the sum of all values:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </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>
Passing parameters by value
Parameters called in a 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.