您现在的位置:首页 >> 前端 >> 内容

ES6箭头函数和扩展的代码实例讲解

时间:2018/2/2 14:27:37 点击:

  核心提示:ES6箭头函数和扩展代码实例讲解// 主动抛出异常function add(a,b = 1){if(a == 0){throw new Error(A is Error);}return a+b;}c...

ES6箭头函数和扩展代码实例讲解

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

ES6箭头函数和扩展的代码实例讲解

function add(a,b = 1){
    if(a == 0){
        throw new Error('A is Error');
    }
    return a+b;
}

// 获取函数参数个数
console.log(add.length);    //  返回1,返回必须传递的参数个数,此处是a

ES6箭头函数和扩展的代码实例讲解

箭头函数

var add = (a,b = 1) => a + b;
document.write(add(1));

var add = (a,b = 2) => {
    document.write('js')
    return a + b;
}
document.write(add(2));

ES6箭头函数和扩展的代码实例讲解

Tags:ES S6 6箭 箭头 
作者:网络 来源:zjsfdx的博客