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

Ste*_* L. 0 javascript arguments increment

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

注意:如果您调用此函数:

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

nth(list, 1)
Run Code Online (Sandbox Code Playgroud)

输出应为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);
    }
}
Run Code Online (Sandbox Code Playgroud)

use*_*688 8

这是因为

 count++
Run Code Online (Sandbox Code Playgroud)

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

你想要前缀.

 ++count.
Run Code Online (Sandbox Code Playgroud)