es6-函数扩展

参数默认值

function test(x,y='world'){ console.log('默认值',x,y); }

test('hello');

test('hello','eeee');

 

这里的world就是默认值,也可以直接传参

但是默认值后面不能再有没有默认值的变量

function test(x,y='world',c)

这种是不行的

再讲一下作用域

{ let x='a'; function test2(x,y=x) { console.log('作用域:',x,y); } }

test2();

test2('b');

 

这里在大括号里有一个x=‘a’;

在作用域外传参也会传x

但是大括号里let x='a';是无效的

test2(1,2)输出的是1,2

但是如果传的参是这样的:function test2(c,y=x){ console.log('作用域:',c,y); }

test2(1)输出的结果会是1,a

这个时候let x='a'就有效了

rest参数

{ function test3(...arg){ for(let v of arg) { console.log('rest',v); } } }

test3(1,2,3,4,'a');

 

rest参数后不能有其他参数了,否则会报错

 

扩展运算符:...

console.log(...[1,2,3])

输出1,2,3

相当于将数组拆成了一个离散的值

 

箭头函数

{ let arrow=v=>v*2;

console.log('arrow',arrow(3));

let arrow2=()=>5;

console.log('arrow2',arrow2());
}

 

这里arrow相当于一个函数

fuction arrow(v){

return v*2;

}

 

尾调用

{ function tail(x)

{ console.log('tail',x); }

function fx(x) { return tail(x); }

fx(123); }

 

这种形式是尾调用,可以提升性能

posted @ 2019-06-25 15:16  ellenxx  阅读(112)  评论(0编辑  收藏  举报