函数默认参数:
当函数没有传递某一个参数时,呈现默认值最优写法:无需在函数中传参
复制代码
1
2
3
4
5function show({x=0,y=0}={}){ console.log(x,y); } show()//不传参不会报错
有弊端的几种写法
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13function show(a,b){ a = a || '欢迎'; b = b || 'mmr'; console.log(a,b); } show('welcome','牧码人'); show('welcome'); show('', '牧码人'); function show(a='欢迎',b='mmr'){ console.log(a,b); } show(,'牧码人');//此处后台会报错
注意: 函数参数默认已经定义了,不能再使用let,const声明
复制代码
1
2
3
4
5
6
7function show(a=18){ let a = 101; //错误 console.log(a); } show()
箭头函数:
复制代码
1
2
3
4
5
6
7
8
9let show = (a,b)=>a+b;//此处相当于return a+b 的结果 console.log(show(12,5)); //另一种写法 let show = (a = 12, b = 5) => { console.log(a, b); return a + b; }; show();
- this问题, 定义函数所在的对象,不在是运行时所在的对象
- 箭头函数里面没有arguments, 用 ‘…’
- 箭头函数不能当构造函数
复制代码
1
2
3
4
5
6let show = (...args) =>{ console.log(args); //console.log(arguments); } show(1,2,3,4,5);
错误写法:针对箭头函数不能当构造函数
复制代码
1
2
3
4
5
6let show = ()=>{ this.name='abc'; } let s=new show(); alert(s.name);//会报错
剩余参数:
展开数组… :
[1,2,3,4] -> … [1,2,3,4] -> 1,2,3,4,5…:
1,2,3,4,5 -> …1,2,3,4,5 -> [1,2,3,4,5]
剩余参数: 必须放到最后
最后
以上就是冷静芒果最近收集整理的关于es6笔记——函数默认参数、箭头函数、剩余参数函数默认参数:箭头函数:剩余参数:的全部内容,更多相关es6笔记——函数默认参数、箭头函数、剩余参数函数默认参数内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复