美文网首页
【学习笔记】ES6 标准 - 函数默认参数、箭头函数、剩余参数

【学习笔记】ES6 标准 - 函数默认参数、箭头函数、剩余参数

作者: Spidd | 来源:发表于2019-05-30 15:59 被阅读0次

ES6 标准 - 函数默认参数、箭头函数、剩余参数

/*
* ES6 标准 - 函数默认参数、箭头函数、剩余参数
* 函数默认参数
* * 【理解】
* * 【解析】
* */
function f({a='a的默认值',b='b的默认值'} = {}) {
    console.log(a,b)
}
f()
/*
* 函数的变化
* * 【注意】函数参数默认已经定义了,不有再使用let,const声明
* * 【理解】
* * 【解析】
*
* */

// function f1(a = 18) {
//     let a = 100;  //报错  a has already been declared
//     console.log(a)
// }
// f1()

/*
* 扩展运算符、Rest运算符 别称剩余运算符 【...】
* * 【理解】能展开数组
* * 【解析】三种用法
     //[1,2,3,4,5]  ...a   1,2,3,4,5
     //1,2,3,4,5  ...a  [1,2,3,4,5]
     //[1,2,3,4,5]   ->  a, b, ...c
*
* */
let arr = ['apple','banana','orange'];
console.log(arr);
console.log(...arr);
// ['apple','banana','orange'] ...['apple','banana','orange'] -> 1,2,3,4,5

function f1(...a) {
    console.log(a)
}
f1(1,2,3,4,5)

function f2(...a) {
    // console.log(Array.prototype.slice.call(arguments).sort())
    console.log(a.sort())
}
f2(1,3,2,5,4)

function f3(a,b,c) {
    // console.log(Array.prototype.slice.call(arguments).sort())
    console.log(a, b, c)
}
f3(...[1, 8, 9])

function f4(a,b,...c) {
    // console.log(Array.prototype.slice.call(arguments).sort())
    console.log(a, b, c)
}
f4(...[1, 8, 9, 8, 9, 10])

/*let arr = [1,2,3,4,5]
let arr2 = [...arr]
console.log(arr,arr2)*/

/*
* 箭头函数
* * 【解析】
() => return
() =>{代码块}
* * 【注意】
1.this问题,定义函数所在的对象,不再是运行时所在的对象
2.箭头函数里面没有 arguments,用'...'剩余运算符   let show=(...args) => {}
3.箭头函数不能当构造函数。
* */
function f5(a,b) {
    return a+b;
}
console.log(f5(1,2))

let f6 = (a,b) => a+b;
console.log(f6(1,2))

let f7 = (a,b) => {
    return a+b;
}
console.log(f7(8,9))

/*
let json = {
    id:1,
    show:function () {
        setTimeout(function () {
            console.log(this.id)
        },2000)
    }
}
json.show() //undefined
*/

let json = {
    id:1,
    show:function () {
        setTimeout(() => {
            console.log('定时:' + this.id)
        },1000)
    }
}
json.show(); //1

let f8 = (...args) => {
    console.log(args.length)
}
f8(1,2,3,4,5,)

相关文章

网友评论

      本文标题:【学习笔记】ES6 标准 - 函数默认参数、箭头函数、剩余参数

      本文链接:https://www.haomeiwen.com/subject/urrvtctx.html