/**
* 函数的扩展
*/
/**
* 函数参数的默认值问题
* 注意:
* 默认值参数的后面,不能再有没有默认值的参数
*/
{
function test(x, y = 'world'){
console.log('默认值',x,y);
}
test('hello');
test('hello','kill');
}
{
let x='test';
/**
* 函数默认参数作用域的问题
* @param {*} x
* @param {*} y=x x在参数1中出现,则这个x的值就是参数1的值,而不取外层定义的x值
*/
function test2(x,y=x){
console.log('作用域',x,y);
}
test2('kill');
}
{ //rest的参数 注意:rest的参数后面不能再有别的参数了
function test3(...arg){ //...arg代表所有的参数
for(let v of arg){
console.log('rest',v);
}
}
test3(1,2,3,4,'a');
}
{ //扩展运算符
console.log(...[1,2,4]); //1 2 4
console.log('a',...[1,2,4]); //a 1 2 4
}
{ //箭头函数 =>
let arrow = v => v*2;
let arrow2 = () => 5;
console.log('arrow',arrow(3));
console.log(arrow2());
}
/**
* 尾调用
* 好处:提升性能(更多技能请自行search)
* 函数的最后一句话是不是一个函数
*/
{
function tail(x){
console.log('tail',x);
}
function fx(x){
return tail(x)
}
fx(123)
}