为什么for(arguments中的var i)没有'不起作用

How come for(var i in arguments) doesn't work?

本文关键字:没有 不起作用 for arguments 中的 var 为什么      更新时间:2023-09-26

我是JavaScript新手。我正试图弄清楚为什么这不起作用:

function myFunction(){
    document.getElementById("result").value=add(1,2);
}
function add(){
    var sum = 0;
    for(var i in arguments)
        sum += i;
    return sum;
}

从而输出CCD_ 1。为什么?

您正在迭代密钥,请执行以下操作:

function add(){
    var sum = 0;
    for(var i in arguments)
        sum += arguments[i];
    return sum;
}

更具体地说,键是字符串"0""1",因此您的响应是初始0和后续键的串联。

此外,如果您对现代平台上的javascript感兴趣,以下内容非常清晰简洁。

function add(){
    return [].reduce.call(arguments, function(a, b) {
        return a+b;
    });
}
console.log(add(1,2));

试试这个:

function add(){
    var sum = 0, x = 0;
    for(var i in arguments){
        //the key is an index
        if(!arguments.hasOwnProperty(i)) continue;
        // try converting argument[i] to a number
        x = +arguments[i];
        // check if argument is a valid number 
        if(!isNaN(x)) {
            sum += x;
        }
    }
    return sum;
}

演示:http://jsfiddle.net/vg4Nq/

这里还有另一种可能性。

function add() {
    var numbers = [].slice.call(arguments),
        sum = 0;
    do {
        sum += numbers.shift();
    } while (isFinite(sum) && numbers.length);
    return +sum;
}
console.log(add(1, 2, 3, 4, 5));

jsfidd

更新:这比reduce更跨浏览器。如果我们希望对数据进行更多的检查,我们也可以对此进行改进。

function add1() {
    var numbers = [].slice.call(arguments),
        sum = 0,
        x;
    do {
        x = numbers.shift();
        sum += (typeof x === "number" ? x : NaN);
    } while (isFinite(sum) && numbers.length);
    return +sum;
}
console.log("add1", add1(1, 2, 3, 4, 5));
console.log("add1", add1(1, 2, 3, 4, 5, ""));
console.log("add1", add1(1, 2, 3, 4, 5, "0xA"));
console.log("add1", add1(1, 2, 3, 4, 5, NaN));
console.log("add1", add1(1, 2, 3, 4, 5, Infinity));
console.log("add1", add1(1, 2, 3, 4, 5, -Infinity));
console.log("add1", add1(1, 2, 3, 4, 5, true));
console.log("add1", add1(1, 2, 3, 4, 5, false));
console.log("add1", add1(1, 2, 3, 4, 5, null));
console.log("add1", add1(1, 2, 3, 4, 5, undefined));
console.log("add1", add1(1, 2, 3, 4, 5, []));
console.log("add1", add1(1, 2, 3, 4, 5, {}));

我们可以将其与不检查进行比较

function add2() {
    return [].reduce.call(arguments, function (a, b) {
        return a + b;
    });
}
console.log("add1", add2(1, 2, 3, 4, 5));
console.log("add1", add2(1, 2, 3, 4, 5, ""));
console.log("add2", add2(1, 2, 3, 4, 5, "0xA"));
console.log("add2", add2(1, 2, 3, 4, 5, NaN));
console.log("add2", add2(1, 2, 3, 4, 5, Infinity));
console.log("add2", add2(1, 2, 3, 4, 5, -Infinity));
console.log("add2", add2(1, 2, 3, 4, 5, true));
console.log("add2", add2(1, 2, 3, 4, 5, false));
console.log("add2", add2(1, 2, 3, 4, 5, null));
console.log("add2", add2(1, 2, 3, 4, 5, undefined));
console.log("add2", add2(1, 2, 3, 4, 5, []));
console.log("add2", add2(1, 2, 3, 4, 5, {}));

jsfidd

这些引入的额外检查对性能有何影响?

让我们看看,jsperf

请随意将其他解决方案添加到性能测试中 -我加了其他的。

无论如何,当循环通过arguments对象时,我会避免使用for..in(以避免循环通过可能附加到arguments的任何方法),并会选择任何其他常见的Array循环方法:for0010、forEachreduce

请参阅相关性:为什么在数组迭代中使用"for…in"是个坏主意?