有一個函數是這樣的:
function Car:setSpeed(t_speed)
self.speed = t_speed
print(self.speed)
end
我准備調用回調函數的函數:
function CarDriver:tapGas(self, callback)
currentSpeed = currentSpeed + 10
callback(currentSpeed)
end
調用過程:
CarDriver:tapGas(Car.setSpeed)
然后我歡快的調用tapGas,踩着油門踏板。。。結果打印的self.speed = nil......
正確的調用過程應該是這樣:
CarDriver:tapGas(function(t_speed)
return Car.setSpeed(t_speed)
end)