ES6 handles default parameters

Function person(name = 123, age = 456){// type = typeof(name) === 'undefined'? 123 : name console.log(name, age) } person() // 123 456 person(null, null) // null nullCopy the code

The default parameter null is a valid value and is used only when the function argument is undefined. Default arguments can also be defined as functions or variables

function obj(){
    return {
    	aa:123,
        bb:456
    }
}
function person(name = obj()){
    console.log(name)
}
person() // {xxx}
person('111') // 111
Copy the code

Parameter expressions are executed only if they are called and no arguments are passed.

function person(name=nickName, nickName){
    console.log(name, nickName)
}
person() // Cannot access 'nickName' before initialization
Copy the code

This is called before the variable is defined, it will report an error, temporary dead zone.

The difference between the arguments

In ES5 non-strict mode, function name parameter changes are reflected in the arguments object, i.e. in non-strict mode it is mapped to parameters.

function test(a, B){console.log(arguments) // [1,2] console.log(b) a = 'a' b = 'b' console.log(arguments) // ['a','b']} Test (1, 2)Copy the code

In strict mode, arguments do not change regardless of how arguments change.

function test(a, Log (a) console.log(b) a = 'a' b = 'b' console.log(arguments) // } [1, 2] test (1, 2)Copy the code

If a function uses default parameter values, the arguments objects behave the same as in strict mode in JavaScript.

Function test(a, b = 2) {a = 12 b = 10 console.log(arguments) // [1]Copy the code

Normally development would be written as… Params gets data outside of known parameters.

function test(a, ... params){ // rest parameter must formal parameter console.log(params) // [3, 5] } test(1, 3, 5)Copy the code

Also, indefinite arguments cannot be defined in setters of object literals, because setter functions take only one function, and when written as indefinite arguments, it will be an array, which will cause program exceptions.

let obj = { set name(... params) {} } // Setter function argument must not be a rest parameterCopy the code
  • Arrow functions have no arguments
let test1 = () => {
    console.log(arguments)
}
test1(123) // arguments is not defined
Copy the code

An arrow function will only search for the arguments object from the outer layer of the non-arrow function. If the outer layer is a non-arrow function and the function has no arguments object, it will break and return

function test2(a, b, c){
    return () => {
    	console.log(arguments) // [1]
    }
}
test2(1)()
Copy the code
  • Arrow function this value

The value of this of the arrow function depends on the value of this of the non-arrow function outside the function. If the arrow function is the same as the arrow function, keep looking. If you can’t find it, this is the window object

let person = { test: () => { console.log(this) }, Fn (){return () => {console.log(this)}}} Person.test () // window person.fn()() // Person objectCopy the code

The arrow function cannot change the this pointer

let person = {}
let test = () => console.log(this)
test.bind(person)()
test.call(person)
test.apply(person)
Copy the code

At precompile time, this is determined.

  • Arrow functions cannot be declared with the new keyword
let test = () => {}
new test() // Uncaught TypeError: test is not a constructor
Copy the code
  • Arrow functions have no prototype

Do all JavaScript functions have the prototype attribute? This is incorrect.

let test = () => {}
test.prototype // undefined
test.__proto__ === Function.prototype // true
Copy the code
Let BBB = (b, b, b) => {} // Uncaught SyntaxError: Duplicate parameter name not allowed in this context let bb = function(b, b, b){} // ES5 does not generate an errorCopy the code

Summary:

  • ES6 handles default arguments, which can be functions or variables
  • Arrow functions have no arguments and their value depends on the value of the outer non-arrow function.
  • This differs, depending on the value of this of the outer non-arrow function
  • No prototype property
  • Arrow functions cannot operate on new