ES6知识点整理——函数扩展
函数新增特性:参数默认值、rest参数、扩展运算符、箭头函数、this绑定、尾调用
1.参数默认值
默认值后面不能有没有默认值的变量
{
function test(x, y = 'world'){
console.log('默认值',x,y);
//hello worldhello kill
}
test('hello');
test('hello','kill');
}
作用域问题
当函数的参数中有设置默认值的参数时,包含参数的圆括号就会形成一个作用域
{
let x='test';
function test2(x,y=x){
console.log('作用域',x,y);
//kill kill
}
test2('kill');
}
{
let x='test';
function test2(c,y=x){
console.log('作用域',x,y);
//kill test
}
test2('kill');
}
2、rest参数
...把输入的参数转成数组
{
function test3(...arg){
for(let v of arg){
console.log('rest',v);
//rest 1 rest 2 rest 3 rest 4 rest a
}
}
test3(1,2,3,4,'a');
}
3、扩展运算符
将一个数组转为离散的值
{
console.log(...[1,2,4]);
//1 2 4
console.log('a',...[1,2,4]);
a 1 2 4
}
4、箭头函数
es5: function a(){} this的指向是该函数被调用的对象
es6: (参数) => {} 参数只有一个可以省略(),花括号中的表达式作为返回值的时候可以省略{} this的指向是定义时this的指向
1.写法上省去了function 2.this的指向有了新的意义
·由于箭头函数不绑定this, 它会捕获其所在(即定义的位置)上下文的this值, 作为自己的this值
·箭头函数没有arguments,apply,call, bind,因为箭头函数根本没有this,所以改变this指向的函数对他无效,
·箭头函数不能作为构造函数,不能使用new;
·不可以使用yield命令,因此箭头函数不能用作 Generator 函数。
{
let arrow = v => v*2;
let arrow2 = () => 5;
console.log('arrow',arrow(3));
//6
console.log(arrow2());
//5}
【ES6知识点整理——函数扩展】5、尾调用
一个函数依赖于另一个函数的操作,建议使用尾调用
{
function tail(x){
console.log('tail',x);
}
function fx(x){
return tail(x)
}
fx(123)//tail 123
}
推荐阅读
- 20190302|20190302 复盘翻盘
- (二)ES6第一节变量(let|(二)ES6第一节变量(let,const)
- 小学英语必考的10个知识点归纳,复习必备!
- 六步搭建ES6语法环境
- 【韩语学习】(韩语随堂笔记整理)
- 三国谋略22(找准你的定位)
- (六)Component初识组件
- 程序员|【高级Java架构师系统学习】毕业一年萌新的Java大厂面经,最新整理
- java|java 常用知识点链接
- 心理学知识点总结(1)