js function ES6 arrow arrow functions (Arrow Functions) JS ES6 arrow function (Arrow Functions) using

First, grammar

Basic grammar

(Parameter 1, parameter 2, ..., parameter N) => {} function declarations
(Parameter 1, parameter 2, ..., parameter N) => Expression (single)
 // corresponds :( parameter 1, parameter 2, ..., parameter N) => {return expression;}

// When only one parameter, parenthesis are optional: 
(a single parameter) => {} function declarations
Single parameter => {} function declarations

// no arguments should be written as a pair of parentheses. 
() => {} Function declarations

Advanced Grammar

// bracketed function returns the object body literal expression: 
parameter => ({foo: bar})

// support remaining parameters and default parameters 
(parameter 1, parameter 2, ... REST) => {} function declarations
(Parameter 1 = default value of 1, parameter 2, ..., N = default value of the parameter N) => {} function declarations

// also supports the argument list deconstruct 
the let F = ([A, B] = [. 1, 2], {X: C = {X}: A + B}) => A + B + C;
f();  // 6

Examples

1. simple function having a parameter

var single = a => a
single('hello, world') // 'hello, world'

2. There is no need to use the parameters before the parentheses arrow plus

were log = () => {
    alert('no param')
}

3. The need to use a plurality of parameters between parentheses, the comma intervals, for example two numbers

var add = (a, b) => a + b
add(3, 8) // 11

4. The body of the function multiple statements need to use braces

var add = (a, b) => {
    if (typeof a == 'number' && typeof b == 'number') {
        return a + b
    } else {
        return 0
    }
}

Need parentheses wrap 5. returns an object, interpreted as braces occupied the code blocks

var getHash = arr => {
    // ...
    return ({
        name: 'Jack',
        age: 33
    })
}

6. directly as an event handler

document.addEventListener('click', ev => {
    console.log(ev)
})

7. callback as an array sort

var arr = [1, 9 , 2, 4, 3, 8].sort((a, b) => {
    if (a - b > 0 ) {
        return 1
    } else {
        return -1
    }
})
arr // [1, 2, 3, 4, 8, 9]

Second, Notes

1. typeof operator and function as normal

var func = a => a
console.log(typeof func); // "function"

2. instanceof也返回true,表明也是Function的实例

console.log(func instanceof Function); // true

3. this固定,不再善变

obj = {
    data: ['John Backus', 'John Hopcroft'],
    init: function() {
        document.onclick = ev => {
            alert(this.data) // ['John Backus', 'John Hopcroft']
        }
        // 非箭头函数
        // document.onclick = function(ev) {
        //     alert(this.data) // undefined
        // }
    }
}
obj.init()

4. 箭头函数不能用new

var Person = (name, age) => {
    this.name = name
    this.age = age
}
var p = new Person('John', 33) // error

5. 不能使用argument

var func = () => {
    console.log(arguments)
}
func(55) // Uncaught ReferenceError: arguments is not defined

 

 

博客搬运地址:

  1. MDN箭头函数
  2. ES6箭头函数(Arrow Functions)
  3. JS ES6中的箭头函数(Arrow Functions)使用

Guess you like

Origin www.cnblogs.com/clement-jiao/p/11073781.html