pop()不是一个函数 – nodejs

当我调用数组中的函数pop()时,我TypeError: cars.pop is not a function一个奇怪的错误,它说TypeError: cars.pop is not a function …我很困惑。

任何帮助? 下面是代码。 谢谢,

 //callback chaining to avoid having multiple callbacks in the event queue //only one callback calling others function showCar(car, callback) { console.log('Saw a ' + car); if (car.length) { //register the function as asynchronous process.nextTick(function() { callback(); }) } } function logCars(cars) { var car = cars.pop(); showCar(car, function() { //chaining of call backs logCars(car); }); } var cars = ['ferrari', 'porsh', 'Hyundai', 'Peugeot']; logCars(cars); 

这是因为你没有将数组传递给第二次调用的logCars函数。 您正在传递第二次recursion调用中popup的string。

换句话说, logCars(car)应该是嵌套callback的logCars(cars)

 function logCars (cars){ var car = cars.pop(); showCar(car, function () { logCars(cars); // This should be `cars`, not `car` like you had }); }