默认值
function add(a, b = 1) {
return a + b;
}
console.log(add(1)) //2
function add(a = 2, b) {
return a + b;
}
console.log(add(1)) //NaN
主动抛出错误
function add(a,b=1){
if(a==0){
throw new Error('this is error')
}
return a+b
}
console.log(add(0))
函数中的严谨模式
ES5中就经常使用严谨模式来进行编程,但是必须写在代码最上边,相当于全局使用。在ES6中我们可以写在函数体中,相当于针对函数来使用。
function add(a,b){
'use strict'
if(a == 0){
throw new Error('this is error')
}
return a+b;
}
console.log(add(1,2)) //3
获得需要传递的参数个数
function add(a, b) {
'use strict'
if (a == 0) {
throw new Error('this is error')
}
return a + b;
}
console.log(add.length) //2
箭头函数
只有一行,可以不写{}
var add = (a,b=1) => a+b;
console.log(add(1))
多行,写{}
var add = (a,b=1) => {
console.log('jie')
return a+b;
}
console.log(add(1))
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。