Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details.

A:

Parameters and arguments

  • A parameter is a variable that is listed when you define a function.
  • Arguments are values passed to a function when it is called.

Look at another example showing parameters and arguments for functions, function expressions, and arrow functions respectively

The remaining parameters

Residual parameters, also known as REST parameters, were introduced in ES6 and have the form… The name of a variable used to get extra arguments to a function.

The rest argument goes with a variable that puts the extra arguments into an array.

function sum (. rest) {
  return rest.reduce((acc, pre) = > acc + pre)
}

console.log(add(1.2.3)) / / 6
Copy the code

Before ES6, there were no arguments left and you could only handle them with arguments objects.

Arguments is a pseudo-array that needs to be converted to an array.

function sum () {
  const args = Array.prototype.slice.call(arguments)  
  return args.reduce((acc, pre) = > acc + pre)
}

console.log(add(1.2.3)) / / 6
Copy the code

Note that the REST argument cannot be followed by any other argument (that is, only the last one), otherwise an error will be reported.

/ / an error
function f(a, ... b, c) {
  // ...
}
Copy the code

The length property of the function, excluding the REST argument.

const fn1 = (a) = > {}
console.log(fn1.length) / / 1

const fn2 = (. a) = > {}
console.log(fn2.length) / / 0

const fn3 = (a, ... b) = > {}
console.log(fn3.length) / / 1
Copy the code

The default parameters

ES6 allows you to set default values for function arguments, which are written directly after the parameter definition.

function log (x, y = 'World') {
  console.log(x, y)
}
log('Hello') // Hello World
log('Hello'.'China') // Hello China
log('Hello'.' ') // Hello
Copy the code

Before ES6, if you wanted to do the same thing, you would say,

function log (x, y) {
  y = y || 'World'
  console.log(x, y)
}
log('Hello') // Hello World
log('Hello'.'China') // Hello China
log('Hello'.' ') // Hello World
Copy the code

In general, the parameter that defines the default value should be the last parameter of the function. Because it’s easier to see what parameters are being omitted. If non-trailing arguments are set to default values, they cannot be omitted.

function f (x = 1, y) {
  return x + y
}
console.log(f(2)) // Output NaN, because the operation is 2 + undefined
console.log(f(undefined.2)) // Output 3, pass undefined, which is the default value
Copy the code

In the example above, x has a default argument. If you call a function and pass only one argument, thinking that y is passed, it will be wrong. The program will only think that you passed x and did not pass y, so y is undefined.

If undefined is passed, this parameter is triggered to equal the default value.

The length property of the function, excluding the default argument

const fn1 = (a) = > {}
console.log(fn1.length) / / 1

const fn2 = (a = 1) = > {}
console.log(fn2.length) / / 0

const fn3 = (a, b, c = 1) = > {}
console.log(fn3.length) / / 2
Copy the code

Implicit parameter

Functions in JS take two implicit arguments, one is arguments and the other is this

function fn () {
  console.log(arguments)
  console.log(this)
}

fn(1.2.3)
Copy the code

Argumentargumentargumentargumentargumentargumentargumentargumentargumentargumentargumentarguments

How to convert pseudo-arrays into arrays in JS

For this, see the link at the end of this article.

At the end

If my article is helpful to you, your 👍 is my biggest support ^_^

I’m Allyn, export insight technology, goodbye!

The last:

“Front-end Daily question (24)” say JS this

Next up:

What is the difference between the arrow function and the normal function?