Today I encountered a weird decorator function that does not have any parameter but used arguments
inside its scope. I thought it's a mistake at first.
Then I defined such a function in node:
noParameterFunction() {
console.log(arguments)
}
calling this function with argument 'a'
would give me the results below:
> noParameterFunction('a')
[Arguments] { '0': 'a' }
undefined
I was expecting an error in which arguments are passed to a function that takes no argument to be thrown, but I got this.
Can anyone explain the rationale behind this design? Thanks.
PS Declaring the function using arrow will fail to reproduce the result below.
The [arguments] object contains an array of the arguments passed to the function.
A parameter is a variable that you define at the same time you define a method and will hold the argument passed to it, an argument is the actual variable you pass to that parameter. You are printing the arguments, that is, the values you pass when you call the function. Since you are calling it with the argument "a", it is printed and can actually be used through the [arguments] object, so it is not an error and won't trigger an exception.
arguments
is an Array-like object accessible inside functions that contains the values of the arguments passed to that function.The
arguments
object is a local variable available within all non-arrow functions.
Can anyone explain the rationale behind this design?
The
arguments
object is useful for functions called with more arguments than they are formally declared to accept. This technique is useful for functions that can be passed a variable number of arguments.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.