我對遞歸函數中增加值的功能有疑問。在遞歸函數中增加計數器
當我使用:
counter++
不起作用
但是當我使用counter + 1
它正常工作。
我還發現它的另一種工作方式:++counter
,但我真的不明白在櫃檯前使用++
有什麼不同。
實施例:
printEachName = (companyNames, newPeople, counter, callback) => {
if (companyNames.length === newPeople.length) {
return callback(false, companyNames);
}
console.log('counter >>> ', counter);
let newP = newPeople[counter];
companyNames.push(newP.name);
printEachName(companyNames, newPeople, counter + 1, callback);
}
printEachName([], newPeople, 0, (errorPrinting, response) => {
if (errorPrinting) {
//res.send()
return;
}
console.log('response is >>> ', response);
});
@Luis,'計數器++'是不一樣的'計數器=計數器+ 1'。 'counter ++'更像'temp = counter,counter = counter + 1,temp'。在JavaScript控制檯中試試這個,你會看到'counter = 123; foo =(temp = counter,counter = counter + 1,temp)'。 'foo'將爲123','counter'爲124 – gman