学习笔记:ES6之函数扩展(非常重要)

来源:互联网 发布:c语言乘法函数是什么 编辑:程序博客网 时间:2024/06/06 21:39

函数新增特性:

参数默认值

(1)默认值

functiontest1(x,y='world'){

console.log("默认值:",x,y);

}

test1();--undefined world

test1('hello');--hello world

(2)作用域的问题

let x='test';

function test2(x,y=x){

console.log("默认值:",x,y);

}

test2('hello');---hello hello

test2()--undefinedundefined

rest参数:把一系列的参数转换成数值,rest参数之后不能有其他的参数(值转换成数组)

参数的形式: …变量名

rest搭配的变量是一个数组,就可以不再使用arguments对象了 

functiontest3(...arg){

for(letv ofarg){

console.log('rest参数',v);

}

}

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

---

rest参数1

rest参数2

rest参数3

 rest参数 4

 rest参数 a

 

扩展运算符:是rest参数的逆运算(将数组转换为用逗号分隔的参数列表)

形式: …

console.log(...[1,2,4]);--124转换成一个个的值

console.log('a',...[1,2,4]);--a 1 2 4

箭头函数(重要)--

函数名 = 函数参数 => 函数返回值

let arrow=v=>v*2;(有参数v

console.log(arrow(3));--6

let arrow=()=>5;(无参数

console.log(arrow());--5

this绑定

箭头函数与this进行绑定,视情况而定

尾调用:提升性能

function tail(x){

console.log('tail',x);

}

function fx(x){

return tail(x);

}

fx(123);

原创粉丝点击