Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么count++;作为参数传递时不起作用_Javascript_Arguments_Increment - Fatal编程技术网

Javascript 为什么count++;作为参数传递时不起作用

Javascript 为什么count++;作为参数传递时不起作用,javascript,arguments,increment,Javascript,Arguments,Increment,我有下面的代码。如果向代码传递一个列表,它将在该位置提供值(索引为零)。这段代码可以工作,但是如果我将count=count+1替换为count++(在条件的最后一个分支中),它将不再工作。有人能帮我理解为什么吗 注意:如果您这样调用函数: var list = {value: 10, rest: {value: 10, rest: {value: 30, rest: null}}} nth(list, 1) 输出应该是20 function nth(list, index, count)

我有下面的代码。如果向代码传递一个列表,它将在该位置提供值(索引为零)。这段代码可以工作,但是如果我将count=count+1替换为count++(在条件的最后一个分支中),它将不再工作。有人能帮我理解为什么吗

注意:如果您这样调用函数:

var list = {value: 10, rest: {value: 10, rest: {value: 30, rest: null}}}

nth(list, 1)
输出应该是20

function nth(list, index, count) {
    if (count === undefined) {
        count = 0;
    }

    if (count === index) {
        return list.value;
    }
    else if (list.rest === null) {
        return undefined;
    }
    else {
        // note that count++ will not work here
        return nth(list.rest, index, count = count + 1);
    }
}
这是因为

 count++
是后缀增量。这意味着它创建一个新值,即旧计数,并将该值传递给函数

你想要前缀

 ++count.
尝试改变

return nth(list.rest, index, count = count + 1);


您的
列表中没有
20
,谢谢您的回答!
return nth(list.rest, index, ++count);