Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
理解LUA回调_Lua_Callback - Fatal编程技术网

理解LUA回调

理解LUA回调,lua,callback,Lua,Callback,例如,此代码: -- Defining a function that takes a callback function as a parameter function HelloMessage(callback) -- Below you are passing the string "sup" as a parameter to the callback function callback("sup") end -- Here, below, we are calling th

例如,此代码:

-- Defining a function that takes a callback function as a parameter
function HelloMessage(callback)
  -- Below you are passing the string "sup" as a parameter to the callback function 
  callback("sup")
end

-- Here, below, we are calling the function defined earlier
-- and we pass a callback function to it (a.k.a. "handler") 
HelloMessage(function(message) 
  print(message) -- "sup" gets printed
end)
我不明白它是怎么工作的。它如何将sup传递到消息参数

HelloMessage(function(message) print(message) end)
基本相当于

do
  local callback = function(message) print(message) end
  callback("sup")
end
do
  local message = "sup"
  print(message)
end
这相当于

do
  local callback = function(message) print(message) end
  callback("sup")
end
do
  local message = "sup"
  print(message)
end
function(message)print(message)end
定义了一个匿名函数,该函数立即被函数参数
callback
引用,它是
HelloMessage
范围内的一个局部变量

cb_function = function( var_txt ) print("var_txt = ", var_txt ) end

function HelloMessage( callback_func )
  if type( callback_func ) == "function" then --just chek is func passed
     callback_func( "Print to callback function" )
  end
end

HelloMessage( cb_function ) --passing call back function
基本相当于

do
  local callback = function(message) print(message) end
  callback("sup")
end
do
  local message = "sup"
  print(message)
end
这相当于

do
  local callback = function(message) print(message) end
  callback("sup")
end
do
  local message = "sup"
  print(message)
end

function(message)print(message)end
定义了一个匿名函数,该函数立即被函数参数
callback
引用,它是
HelloMessage
范围内的一个局部变量。

我建议在这里运行并修改代码,进行一点实验;)干杯。-我建议在这里运行和修改代码,进行一点实验;)干杯-
cb_function = function( var_txt ) print("var_txt = ", var_txt ) end

function HelloMessage( callback_func )
  if type( callback_func ) == "function" then --just chek is func passed
     callback_func( "Print to callback function" )
  end
end

HelloMessage( cb_function ) --passing call back function