In the functional code, using a special object called arguments, developers can access arguments without explicitly specifying their names.
For example, in the function sayHi(), the first argument is message. This value is also accessible with arguments[0], which is the value of the first argument (the first argument is at position 0, the second argument is at position 1, and so on).
Thus, we can override the function without explicitly naming parameters:
function sayHi() {
if (arguments[0]
== "bye") { return; } alert(arguments[0]); }
Number of test parameters
You can also use the arguments object to detect the number of arguments to a function by referring to the arguments.length property.
The following code prints the number of arguments used per call to the function:
function howManyArgs() {
alert(arguments.length);
}
howManyArgs("string", 45);
howManyArgs();
howManyArgs(12);
The above code displays “2”, “0”, and “1” in turn.
Note: Unlike other programming languages, ECMAScript does not verify that the number of arguments passed to a function is equal to the number of arguments defined by the function. A developer-defined function can take any number of arguments (up to 255, according to Netscape documentation) without throwing any errors. Any missing arguments are passed to the function as undefined, and extra functions are ignored.
Analog function overloading
To simulate function overloading, use the arguments object to determine the number of arguments passed to the function:
function doAdd() {
if(arguments.length == 1) {
alert(arguments[0] + 5);
} else if(arguments.length == 2) {
alert(arguments[0] + arguments[1]);
}
}
doAdd(10); //output "15"
doAdd(40, 20); //output "60"
When there is only one argument, the doAdd() function increments the argument by 5. If there are two arguments, the two arguments are added and their sum is returned. So, doAdd(10) outputs “15”, while doAdd(40, 20) outputs “60”.
It’s not as good as overloading, but it’s good enough to get around this limitation of ECMAScript.
Function overview Function object
Read More:
- JS to determine whether the string contains a character
- JS native implementation Promise.all
- How to Fix “HTTP 405 method not allowed” Error
- Application of call, apply and bind methods
- Localstorage sets the expiration time.
- H5 page left and right stretch content area
- Openmv identifies two-dimensional code, color recognition and serial communication
- TypeScript error TS2345: Argument of type ‘String ‘is not assignable to parameter of type ‘string’
- Vue refreshes the current page (no flash screen will appear)
- JS to find the last character of the string and remove it
- If the request parameter is formdata, use the Ajax operation
- JavaScript determines whether parentheses are paired.
- How to Block a frame with origin from accessing a cross origin frame
- JavaScript determines whether it’s morning or afternoon or evening
- Vue: How to Fix “not displaying the holder in IE9 and below”
- [ERR_INVALID_ARG_TYPE]: The “path“ argument must be of type string. Received undefined error
- Bugly automatically upload script and report zip error: nothing to do! In xcode10
- JS uses onerror to automatically catch exceptions
- Asynchronous loading method of Baidu map
- Difference between contenttype and datatype in Ajax request of jquery