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:
- [Solved] Error: Incorrect arguments to mysqld_stmt_execute
- [Solved] Vue3.2 Error: Object.fromEntries is not a function
- [Solved] Uncaught DOMException: Failed to execute ‘readAsDataURL‘ on ‘FileReader‘: The object is already busy
- [Vue warn]: Error in v-on handler: “TypeError: Object(…) is not a function“
- [Web Browser] “Uncaught TypeError: object is not a function“
- [Solved] Module build failed TypeError this.getOptions is not a function at Object.loader
- [Solved] electron-vue project error: Object.fromEntries is not a function electron-vue
- [Solved] uni app TypeError: undefined is not an object (evaluating ‘modules[moduleId].call‘) __ERROR
- How to Test whether the property value of each property in the object is not empty
- Using for in loop complex data types (object and array) in ES6
- JS to determine whether the string contains a character
- Google browser plug-in JavaScript errors Notifier
- JavaScript Common Errors List (Reasons & Solutions)
- uniapp [Vue warn]: Error in onLoad hook: “TypeError: Attempting to change the setter of an unconfigu
- JS native implementation Promise.all
- How to Fix “HTTP 405 method not allowed” Error
- [Solved] Vue item error: Regeneratorruntime is not defined
- [How to Solve] TypeError: XXX is read-only
- How to Solve JS error: Unexpected end of JSON input,Unexpected token u in JSON at position 0
- [Solved] Echart Error: Typeerror: axis Getaxesonzeroof is not a function