ECMAScript 6知识点总结 --- 函数

  • 函数默认参数
    let func = (x = 1, y = 2) => {
        console.log(x ,y)
    }
    func()
    // 1, 2
    
    let func2 = ({x=0, y=0}={}) => {
        console.log(x, y)
    }
    func2()
    // 0, 0
    func2({x:1, y:2})
    // 1, 2
  • 函数默认参数已经定义了,不能再次声明
    let func = (x=1, y=2) => {
        let x = 2  // 错误
        console.log(x ,y)
    }
    func()
    // Uncaught SyntaxError: Identifier 'x' has already been declared
    
  • 与...结合使用
    let func = (...a) => {
        console.log(a)
    }
    func(1,2,3,4,5)
    // [1,2,3,4,5]
    
    
    let func = (a,b,c) => {
        console.log(a,b,c)
    }
    func(...[1,2,3])
    // 1,2,3
    
    
    // 剩余运算符 必须在最后一个参数
    let func = (a,b,...c) => {
        console.log(a,b,c)
    }
    func(1,2,3,4,5)
    // 1,2,[3,4,5]
    

猜你喜欢

转载自blog.csdn.net/qq_35415374/article/details/83181160