ES6 -箭头函数 ,对象的函数解构

ES6 -箭头函数:

//es6 中的箭头函数和扩展

//es5的写法
   // function add(a,b){
   //    return a + b;
   // }
   // add(1,2); //3

    function add1(a,b=1){
        if(a==0){
            throw new Error('a is zero');
        }
        return a+b;
    }
    //console.log(add1(0)) //Uncaught Error: a is zero 主动抛出异常

    //严谨模式
    function add(a,b){
        'use strict'
        return a + b;
    }
  console.log(add(1)) //NaN
  console.log(add(1,2)) //3

//   获取参数个数 
    console.log(add.length)  //2

    function add2(a,b=1){
        return a + b;
    }
    console.log(add2.length) //1 获取的参数是必须传递参数的个数 当给a复制后 输出 0

//箭头函数  

//箭头函数中不允许使用new的  箭头函数中不能写构造函数
// 方法体只有一行的时候 不用加上花括号
var add3 = (a,b=1) => a+b;
console.log(add3(2)) //3

// 方法体有多行的时候 需要加上花括号
var add4 = (a,b) => {
    console.log( "---");
    return a + b;
}
console.log(add4(1,2)) //3

 对象的函数解构

//对象的函数解构
let json = {
    'a': 'ananiah',
    'b': '杰森' 
}
console.log(json.a) // 输出 ananiah
function fun({a,b = 'default'}){
    console.log(a,b) //输出 ananiah  杰森
}
fun(json);

 

转载于:https://www.cnblogs.com/Ananiah/p/11070323.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值