定义方式1:
1 function abs(x) { 2 if (x >= 0) { 3 console.log(x); 4 } else { 5 console.log(-x) 6 } 7 }
定义方式2:
1 let abs1 = function (a) { 2 if (a >= 0) { 3 console.log(a); 4 } else { 5 console.log(-a) 6 } 7 }
function (a)是一个匿名函数,赋值给了abs1变量
arguments是js免费赠送的一个关键字,存储了传进来的所有参数,是一个数组,可以用遍历的方式来取出参数
1 for (let i = 0; i < arguments.length; i++) { 2 console.log(arguments[i]); 3 }