1.通过内置方法 arguments
function sum(){
let count = 0
for(let i = 0 ; i < arguments.length ; i++){
if(isNaN(arguments[i])){
continue
}
else{
arguments[i] = Number(arguments[i])
count = arguments[i]+ count
}n
}
return count
}
sum(1,1,1,1,1)
console.log(sum(1,2,3,5,'ww',5,'7'));
2.ES6 新增语法 …args 运算符
function sum(...args) {
let count = 0
for (let i = 0; i < args.length; i++) {
if (isNaN(args[i])) {
continue
}
else {
args[i] = Number(args[i])
count = args[i] + count
}
}
return count
}
sum(1, 1, 1, 1, 1)
console.log(sum(1, 2, 3, 5, 'ww', 5, '7'));