ES6学习总结是自己在学习过程中的总结,记笔记就是为了督促自己学习和复习,好记性不如烂笔头。如果有错误,感谢指出。
参数默认值
参数在es5中没有默认值,需要用||自己加上;
默认值后面不能再有没有默认值的变量
{
function test(x, y = 'world'){
console.log('默认值',x,y);
}
test('hello');//hello world
test('hello','kill');//hello kill
}
注意:
{
let x='test';
function test2(x,y=x){
console.log('作用域',x,y);
}
test2('kill');//kill kill
//这里涉及到作用域的问题 函数里面具有单独的作用域 只有没有x的时候 才会继承let所声明的x
}
rest参数(...)
将一系列离散的值转化成数组,同样rest后面不可以再有参数
{
function test3(...arg){
for(let v of arg){
console.log('rest',v);
}
}
test3(1,2,3,4,'a');
}
扩展运算符(...)
将一个数组,转化成一系列离散的值
{
console.log(...[1,2,4]);
console.log('a',...[1,2,4]);
}
箭头函数
{
let arrow = v => v*2;//arrow函数名 返回值
let arrow2 = () => 5;
console.log('arrow',arrow(3));//6 //arrow函数参数
console.log(arrow2());//5
}
尾调用
存在于函数式编程的概念,return是一个函数
好处之一:提升性能
{
function tail(x){
console.log('tail',x);
}
function fx(x){
return tail(x)
}
fx(123)// tail 123
}
网友评论