Lua 函数作为参数传递时的注意事项

有一个函数是这样的:

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)

原文地址:https://www.cnblogs.com/vokie/p/3641264.html