1.第一种方式定义函数
function abs(x) {
if (x >= 0)
return x;
else return -x;
}
2.第二种方式定义函数
var abc = function (x) {
if (x >= 0)
return x;
else return -x;
}
3.arguments使用
当使用该函数的时候,传入的参数超过该方法的参数,也不会报错,全部的参数返回arguments数组
function abs(x) {
for(let i = 0;i<arguments.length;i++){
console.log(arguments[i]);
}
if (x >= 0)
return x;
else return -x;
}
使用示例:
4 …rest使用
…rest放在参数的最后面,他会获取其他参数(不包括已定义的参数),返回一个集合
var abc = function (x,a,...rest) {
console.log(x);
console.log(a);
console.log(rest);
console.log(rest[2]);
return x;
}
使用示例: