function add(a,b){
//'user strict'
if(a == 0){
throw new Error('this is error');
}
return a+b
}
console.log(add(1,4)); //5
利用箭头函数
//利用箭头函数
var add = (a,b=2) => a+b ;
console.log(add(3)) //5
方法体中如果有两句话,那就在方法体外加{}
var add = (a,b=3) => {
console.log('judy');
return a+b
};
console.log(add(3)); //6
console.log(add(1,3)); //4
console.log(add(1,5)); //6
箭头函数包含块语句
// ES5
$("#confetti-btn").click(function (event) {
playTrumpet();
fireConfettiCannon();
});
// ES6
$("#confetti-btn").click(event => {
playTrumpet();
fireConfettiCannon();
});