为什么在回调console.logs生成结果时结果= array [0]或1而不是array [1]或2?

    function test(array, callback) {
        var startingIndex = 0;

        var result = array[startingIndex];

        startingIndex++;

        callback(result);

    }

        test([1, 2, 3], function(result) {
            console.log(result);
    });

最佳答案

这是因为在分配startingIndex变量之前要先递增result变量。

你有:

var result = array[startingIndex];
startingIndex++;


交换这两行,您将获得预期的结果:

startingIndex++;
var result = array[startingIndex];

09-25 18:02