2020-11-01

// ES6允许使用(=>)定义函数
let fn = (a, b) => {
    return a + b
}
// 隐式返回
// let fn=a=>a*333
console.log(fn(1, 2))
// this是静态的始终指向声明作用域下的this的值
this.name = '王二麻子'
function fn1() {
    console.log(this.name)
    return this.name
}
let fn2 = () => {
    console.log(this.name)
    return this.name
}
const json = {
    name: '北京大学'
}
console.log(this)
fn1();
fn2();
fn1.call(json);
fn2.call(json);

// 不能作为构造函数实例化对象,不能使用arguments变量
// 简写省略小括号:形参有且只有一个
// 简写{}:代码体只有一条语句,此时return必须省略
let fn3=a=>a*333

let add=a=>console.log(a*a);
let add1=a=>a*a;
add(3)
console.log(add1(3))
// 绑定addEventListener相关事件是不能使用箭头函数
// 箭头函数适合于this无关的回调:定时器,数组方法的回调;不适合与this有关的回调:dom事件,对象的方法
// 例子
const arr=[1,2,3,4,11,44,54,22]
// 返回偶数
const result= arr.filter(item=>item%2===0);
console.log(result)

// ES6允许给函数的参数赋值初始值,一般位置靠后
let sum = (a,b,c=10)=>a+b+c;
console.log(sum(1,5,6))
console.log(sum(1,5))

// 与结构赋值结合使用
let connect=({a,b,c,d=10})=>{
    console.log(a,b,c,d)
}
connect({a:10,b:3,c:4,d:5})
connect({a:10,b:3,c:4})


// ES6引入rest参数,代替arguments
// es5方式,Array.from可以把伪数组转换为真正的数组
function fn4(){
    console.log(Array.from(arguments))
}
// es6方式
let fn5=(...args)=>{
    console.log(args)
}
let fn6=(a,b,...args)=>{
    console.log(args)
}
fn4(1,2,3)
fn5(1,2,3)
fn6(1,2,5,6)


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值