
参数默认值
{
function test(x, y = 'world'){
console.log(x,y);
}
test('hello'); //hello world
test('hello','kill'); //hello kill
}
注: 默认值后面不能再定义非默认值
function test(x, y = 'world',z){
//但是为什么不报错。。。
}
{
let x='test';
function test1(x,y=x){
console.log('作用域',x,y);
}
test1('kill'); //kill kill //x=kill y=x=kill
test1(); //undefined undefined x=undefined y=x=undefined 这里x是从函数参数拿的,不是函数外部的x
function test2(c,y=x){
console.log('作用域',c,y);
}
test2('kill'); //kill test //c=kill y=x=test 这里x是从函数外拿
}
{
function test3(...arg){ //arg后面不能再有参数,否则报错
for(let v of arg){
console.log('rest',v);
}
}
test3(1,2,3,4,'a'); // arg [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(3)); //6
console.log(arrow2()); //5
}
伪调用
{
function tail(x){
console.log('tail',x);
}
function fx(x){
return tail(x)
}
fx(123) //tail 123
}




近期评论