Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ES6箭头函数 #138

Open
liushaozhen opened this issue Sep 25, 2016 · 0 comments
Open

ES6箭头函数 #138

liushaozhen opened this issue Sep 25, 2016 · 0 comments

Comments

@liushaozhen
Copy link
Member

liushaozhen commented Sep 25, 2016

ES6箭头函数

ES6可以使用“箭头”(=>)定义函数,注意是函数,不要使用这种方式定义类(构造器)。

语法

具有一个参数的简单函数

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

没有参数的需要用在箭头前加上小括号

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

多个参数需要用到小括号,参数间逗号间隔,例如两个数字相加

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

函数体多条语句需要用到大括号

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

返回对象时需要用小括号包起来,因为大括号被占用解释为代码块了

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

直接作为事件handler

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

作为数组排序回调

    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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant