{"id":112,"date":"2023-06-09T19:17:33","date_gmt":"2023-06-09T11:17:33","guid":{"rendered":"https:\/\/yanghanwen.cn\/?p=112"},"modified":"2023-06-09T19:17:33","modified_gmt":"2023-06-09T11:17:33","slug":"%e5%85%b3%e4%ba%8e-lua-%e7%9a%84-function","status":"publish","type":"post","link":"https:\/\/yanghanwen.cn\/?p=112","title":{"rendered":"\u5173\u4e8e lua \u7684 function"},"content":{"rendered":"<ul>\n<li><a href=\"#\u53c2\u8003\u94fe\u63a5\">\u53c2\u8003\u94fe\u63a5<\/a><\/li>\n<li><a href=\"#\u5173\u4e8e\u591a\u8fd4\u56de\u503c\">\u5173\u4e8e\u591a\u8fd4\u56de\u503c<\/a>\n<ul>\n<li><a href=\"#\u56fa\u5b9a\u53c2\u6570\">\u56fa\u5b9a\u53c2\u6570<\/a><\/li>\n<li><a href=\"#\u6307\u5b9a\u8fd4\u56de\u503c\">\u6307\u5b9a\u8fd4\u56de\u503c<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#\u5173\u4e8e-\">\u5173\u4e8e <code>...<\/code><\/a>\n<ul>\n<li><a href=\"#arg-table\">arg table<\/a>\n<ul>\n<li><a href=\"#positional\">positional<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#unpack\">unpack<\/a><\/li>\n<li><a href=\"#pack\">pack<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#self-\u548c-\u7b2c\u4e00\u7c7b\u503c\u7684\u610f\u4e49\">self \u548c \u7b2c\u4e00\u7c7b\u503c\u7684\u610f\u4e49<\/a>\n<ul>\n<li><a href=\"#\u5f15\u7528\u4f20\u9012\">\u5f15\u7528\u4f20\u9012<\/a><\/li>\n<li><a href=\"#\u6a21\u62df\u9762\u5411\u5bf9\u8c61\">\u6a21\u62df\u9762\u5411\u5bf9\u8c61<\/a><\/li>\n<li><a href=\"#\u5192\u53f7\u8bed\u6cd5\u7cd6\">\u5192\u53f7\u8bed\u6cd5\u7cd6<\/a><\/li>\n<li><a href=\"#\u95ed\u5305\u95ee\u9898\">\u95ed\u5305\u95ee\u9898<\/a>\n<ul>\n<li><a href=\"#\u95ed\u5305\u7684\u6982\u5ff5\">\u95ed\u5305\u7684\u6982\u5ff5<\/a><\/li>\n<li><a href=\"#\u59d4\u6258\u548cself\u4f20\u9012\u7684\u95ee\u9898\">\u59d4\u6258\u548cself\u4f20\u9012\u7684\u95ee\u9898<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#\u533f\u540d\u65b9\u6cd5\">\u533f\u540d\u65b9\u6cd5<\/a><\/li>\n<li><a href=\"#\u5d4c\u5957\u8c03\u7528\u7684\u7ec6\u8282\">\u5d4c\u5957\u8c03\u7528\u7684\u7ec6\u8282<\/a><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h1>\u53c2\u8003\u94fe\u63a5<\/h1>\n<p><a href=\"https:\/\/www.lua.org\/pil\/5.2.html\">Programming in Lua : 5.2<\/a><\/p>\n<h1>\u5173\u4e8e\u591a\u8fd4\u56de\u503c<\/h1>\n<h2>\u56fa\u5b9a\u53c2\u6570<\/h2>\n<p>lua \u4e2d\u5141\u8bb8\u8fd4\u56de\u591a\u4e2a\u503c\uff0c\u5e76\u4e14\u8fd4\u56de\u987a\u5e8f\u662f\u56fa\u5b9a\u7684\uff0c<br \/>\n\u8fd4\u56de\u503c\u53ef\u4ee5\u88ab\u591a\u4e2a\u58f0\u660e\u7684 local \u53d8\u91cf\u63a5\u6536\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-LUA\">function r()\n    return \"v1\", \"v2\", \"v3\"\nend\n\nlocal a, b, c, d = r()\nprint(a, b, c, d)\n\n-&gt; v1   v2  v3  nil<\/code><\/pre>\n<p>\u540c\u6837\u7684\uff0c\u51fd\u6570\u5728\u6b63\u5e38\u60c5\u51b5\u4e0b\u58f0\u660e\u7684 <code>\u56fa\u5b9a\u53c2\u6570<\/code>\uff0c\u4e5f\u53ef\u4ee5\u7528\u8fd9\u4e9b\u8fd4\u56de\u503c\u586b\u5145\uff1a<\/p>\n<pre><code class=\"language-LUA\">function r()\n    return \"v1\", \"v2\", \"v3\"\nend\n\nfunction p(a, b, c)\n    print(a, b, c)\nend\n\np(r())\n\n-&gt; v1   v2  v3<\/code><\/pre>\n<h2>\u6307\u5b9a\u8fd4\u56de\u503c<\/h2>\n<p>\u5982\u679c\u53ea\u9700\u8981\u4f7f\u7528\u7b2c\u4e8c\u4e2a\u53d8\u91cf\uff0c\u53ef\u4ee5\u7528\u7ea6\u5b9a\u4fd7\u6210\u7684\u5199\u6cd5\u5199\u4e00\u4e2a <code>\u5f03\u5143(dummy)<\/code><\/p>\n<pre><code class=\"language-LUA\">local _, x = string.find(s, p)<\/code><\/pre>\n<p>\u4e5f\u53ef\u4ee5\u4f7f\u7528 select \u6307\u5b9a\u9700\u8981\u7528\u7684\u8fd4\u56de\u503c<\/p>\n<pre><code class=\"language-lua\">-- select(number, func)\nprint( select(1, string.find(\"hello world\", \" hel\")))\nprint( select(2, string.find(\"hello world\", \" hel\")))<\/code><\/pre>\n<h1>\u5173\u4e8e <code>...<\/code><\/h1>\n<p><code>...<\/code> \u4f5c\u4e3a\uff08\u53c2\u6570\u6570\u91cf\uff09\u53ef\u53d8\u957f\u7684\u53c2\u6570\uff0c\u79f0\u4e3a <code>\u4e0d\u5b9a\u53c2\u6570<\/code>\uff0c \u7c7b\u4f3c\u4e8e <code>C#<\/code> \u4e2d\u7684 <code>params<\/code> \u5b9a\u4e49\uff0c\u9700\u8981\u653e\u5728\u56fa\u5b9a\u957f\u53c2\u6570\u540e\u9762\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-LUA\">function g(a, b, ...) end<\/code><\/pre>\n<h2>arg table<\/h2>\n<p>lua \u7528 <code>...<\/code> \u6307\u4ee3\u8c03\u7528\u65b9\u6cd5\u65f6\u4ea7\u751f\u7684\u4e00\u7cfb\u5217\u53c2\u6570\uff0c\u5176\u5728 function \u4e2d\u9690\u5f0f\u5730\u4f5c\u4e3a <code>arg<\/code> \u53d8\u91cf(table\u7c7b\u578b)\uff0c<br \/>\n\u5b83\u7684 key \u662f\u53c2\u6570\u7684\u5e8f\u53f7\uff08\u4ece1\u5f00\u59cb\uff09\uff0cvalue \u662f\u53c2\u6570\u7684\u503c\uff08\u4fdd\u6301\u539f\u6709\u7c7b\u578b\uff09<br \/>\n\u4e14 arg \u62e5\u6709\u4e00\u4e2a n \u6210\u5458\u6307\u4ee3\u53c2\u6570\u6570\u91cf\uff08\u56e0\u6b64\u5f53\u4f60\u904d\u5386\u6574\u4e2a table \u65f6\u4f1a\u989d\u5916\u6253\u5370\u4e00\u4e2a n \u7684\u952e\u503c\u5bf9\uff09<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-LUA\">function sample(fixed, ...)\n    print(\"\u56fa\u5b9a\u53c2\u6570\uff1a\"..fixed)\n\n    print(\"\u4e0d\u5b9a\u53c2\u6570\u957f\u5ea6\uff1a\", arg.n)\n\n    for k,v in pairs(arg) do\n        print(\"key:\"..k, \"tvalue:\"..tostring(v), \"ttype:\"..type(v))\n    end\nend\n\nsample(\n\"fixed\",\n\"hello world\", 9999, false, 3.0\n)<\/code><\/pre>\n<p>\u4f7f\u7528\u65f6\uff0c\u76f4\u63a5\u8bbf\u95ee\u53c2\u6570\u4f4d\u7f6e\u5373\u53ef\uff1a<\/p>\n<pre><code class=\"language-LUA\">function r(...)\n    print(\"\u53c2\u65701\", arg[1]);\n    print(\"\u53c2\u65703\", arg[3]);\nend\n\nr(1)<\/code><\/pre>\n<p>\u7531\u4e8e lua \u7684\u7279\u6027\uff0c\u5373\u4f7f\u7528\u6237\u6ca1\u6709\u586b\u5165 \u53c2\u65703 \u4e5f\u4e0d\u4f1a\u5f15\u53d1\u5f02\u5e38<\/p>\n<h3>positional<\/h3>\n<p>lua \u5929\u751f\u4e0d\u652f\u6301 positional \u8fd9\u79cd\u64cd\u4f5c<br \/>\n\u4f46\u53ef\u4ee5\u501f\u52a9 <code>os.rename<\/code> \u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd<br \/>\n\u53c2\u8003\uff1a <a href=\"https:\/\/www.lua.org\/pil\/5.3.html\">https:\/\/www.lua.org\/pil\/5.3.html<\/a><\/p>\n<h2>unpack<\/h2>\n<p><code>arg<\/code> \u53d8\u91cf\u662f lua \u9ed8\u8ba4\u5c06 <code>...<\/code> \u8fdb\u884c <code>\u6253\u5305(pack)<\/code> \u540e\u7684\u4e00\u4e2a <code>table<\/code><br \/>\n\u5982\u679c\u4f60\u60f3\u5c06 <code>...<\/code> \u4e2d\u53ef\u53d8\u957f\u7684\u53c2\u6570\uff0c\u4f5c\u4e3a\u56fa\u5b9a\u53c2\u6570\u8c03\u7528\u53e6\u4e00\u4e2a\u65b9\u6cd5\uff0c\u5219\u9700\u8981\u8fdb\u884c <code>\u89e3\u5305(unpack)<\/code><br \/>\n\u89e3\u5305\u4e00\u822c\u7528\uff1a<\/p>\n<pre><code class=\"language-LUA\">unpack(arg)<\/code><\/pre>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-LUA\">function send(...)\n    print(arg.n)\n    local a, b, c = unpack(arg)\n    print(a, b, c)\nend\n\nsend(\"a\", \"b\");\n\n-&gt; 2\n-&gt; a    b   nil<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528 <code>unpack<\/code> \u5b8c\u6210\u5bf9\u4e0d\u5b9a\u957f\u53c2\u6570\u7684\u8f6c\u6362\u540e\uff0c\u5f97\u5230\u7684\u591a\u4e2a\u8fd4\u56de\u503c\u53ef\u4ee5\u50cf function \u7684\u8fd4\u56de\u503c\u90a3\u6837\u88ab\u591a\u4e2a local \u53d8\u91cf\u63a5\u6536<\/p>\n<pre><code class=\"language-LUA\">function func(arg1, arg2, arg3)\n    print(\"arg1:\", arg1);\n    print(\"arg2:\", arg2);\n    print(\"arg3:\", arg3);\nend\n\nfunction send(...)\n    func(unpack(arg))\nend\n\nsend(\"a\", \"b\");<\/code><\/pre>\n<h2>pack<\/h2>\n<p>arg \u53c2\u6570\u76f8\u5f53\u4e8e\u9ed8\u8ba4\u5bf9 <code>...<\/code> \u8fdb\u884c\u6253\u5305\u540e\u4ea7\u751f\u7684\u4e00\u4e2a <code>table<\/code>\uff0clua \u63d0\u4f9b\u4e86\u4e00\u4e2a <code>table.pack<\/code> \u51fd\u6570<sup id=\"fnref1:REF1\"><a href=\"#fn:REF1\" class=\"footnote-ref\">1<\/a><\/sup> \u5141\u8bb8\u4f60\u5728 function \u53c2\u6570\u4ee5\u5916\u7684\u5176\u4ed6\u5730\u65b9\u624b\u52a8\u8fdb\u884c\u6253\u5305\uff0c<\/p>\n<p>\u4f46\u662f\u8fd9\u4e2a\u6253\u5305\u51fd\u6570\u4f1a\u5728\u4f7f\u7528\u8005\u4f20\u5165 nil \u65f6\u904d\u5386\u4e2d\u65ad\u800c\u4ea7\u751f\u975e\u9884\u671f\u7ed3\u679c\uff0c\u56e0\u6b64\u5f88\u591a\u5730\u65b9\u4f1a\u7528\u4e00\u79cd <code>SafePack<\/code> \u7684\u65b9\u5f0f\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-LUA\">---@param ... any \n---@return table\nSafePack = function(...)\n    local t = {...}\n    -- select('#', ...) \u8fd4\u56de \u4e0d\u5b9a\u53c2\u6570 \u7684\u6570\u91cf\n    t.n = select('#', ...)\n    return t \nend<\/code><\/pre>\n<h1>self \u548c \u7b2c\u4e00\u7c7b\u503c\u7684\u610f\u4e49<\/h1>\n<h2>\u5f15\u7528\u4f20\u9012<\/h2>\n<p>lua \u8bed\u8a00\u4e2d\u7684 function \u4f5c\u4e3a <a href=\"https:\/\/www.lua.org\/pil\/6.html\">\u7b2c\u4e00\u7c7b\u503c<\/a> \u548c\u5e38\u89c4\u7684 string\u3001number \u662f\u4e00\u6837\u7684\u5730\u4f4d<br \/>\n\u53ef\u4ee5\u628afunction\u7684\u8d4b\u503c\u7406\u89e3\u4e3a\u4f20\u9012\u4e86\u4e00\u4e2a\u6307\u9488<br \/>\n\u5f53\u6211\u4eec \u201c\u8c03\u7528\u201d \u8fd9\u4e2a\u6307\u9488\u7684\u5185\u5bb9\u65f6\uff0c\u8c03\u7528\u8005\u53ef\u4ee5\u4f20\u9012\u53c2\u6570\u8fdb\u53bb\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-LUA\">local fun = print\nfun(\"hello world\")<\/code><\/pre>\n<p>\u5b83\u548c\u666e\u901a\u503c\u7c7b\u578b\u7684\u533a\u522b\u5728\u4e8e\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a <code>\u6784\u9020<\/code> \u7684\u80fd\u529b\uff0c<br \/>\n\u5c31\u597d\u50cf table \u53ef\u4ee5\u7528 <code>{}<\/code> \u8fdb\u884c <code>\u6784\u9020<\/code>\uff0c \u8fd9\u4e24\u79cd\u6982\u5ff5\u662f\u4e00\u6837\u7684\uff08\u4ee5\u81f3\u4e8e\u4f60\u53ef\u4ee5\u5728 table \u6784\u9020\u65f6\u58f0\u660e\u4e00\u4e2a function\uff0c\u53c2\u8003\u4e0b\u6587\uff09<\/p>\n<p>\u56e0\u4e3a function \u672c\u8eab\u662f\u4e00\u4e2a\u503c\u7c7b\u578b\uff0c\u56e0\u6b64\u8fd9\u79cd\u8c03\u7528\u65b9\u5f0f\u4e5f\u662f\u53ef\u4ee5\u7684\uff1a<\/p>\n<pre><code class=\"language-LUA\">-- \u4f5c\u4e3a table \u7684\u4e00\u4e2a\u6210\u5458\ninstance[\"fun\"](123)\n\n-- \u53d6\u5b83\u7684\u5f15\u7528\nlocal func = instance[\"fun\"]\nfunc(123)<\/code><\/pre>\n<h2>\u6a21\u62df\u9762\u5411\u5bf9\u8c61<\/h2>\n<p>\u4f8b\u5982\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u6211\u4eec\u6307\u5b9a\u4e00\u4e2a <code>local function<\/code><\/p>\n<pre><code class=\"language-LUA\">local function localFun(caller, value)\n    print(caller.name, value)\nend\n\nlocal instance = {}\ninstance.name = \"some instance\"\ninstance.fun = localFun\n\ninstance.fun(instance, \"hello world\")<\/code><\/pre>\n<p>\u8fd9\u65f6\uff0c\u4e3a\u4e86\u8ba9 \u201c\u65e0\u4e3b\u201d \u7684 local function \u80fd\u591f\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u548c\u5b9e\u4f8b\u8fdb\u884c\u7ed1\u5b9a\uff0c\u6211\u4eec\u7ea6\u5b9a\u4fd7\u6210\u5730\u4f1a\u5c06\u5b9e\u4f8b\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\u4f20\u5165<br \/>\n\u4f46\u6bcf\u4e2a\u51fd\u6570\u5bf9\u4e8e\u5b9e\u4f8b\u7684\u53eb\u6cd5\u58f0\u660e\u53ef\u80fd\u6ca1\u6709\u89c4\u8303\uff0c\u6709\u7684\u4f1a\u53eb this\u3001self\u3001instance\u3001caller\u2026\u2026<br \/>\nlua \u81ea\u5e26\u4e00\u79cd\u8bed\u6cd5\u7cd6\u6765\u89c4\u8303\u8fd9\u79cd\u8c03\u7528\uff08\u5e76\u4e0d\u662f\u7279\u6b8a\u673a\u5236\uff09<\/p>\n<h2>\u5192\u53f7\u8bed\u6cd5\u7cd6<\/h2>\n<p>\u4e0a\u9762\u63d0\u5230\u7684\u5199\u6cd5\u5728 lua \u91cc\u6709\u8bed\u6cd5\u7cd6\uff0c\u5373\uff1a<\/p>\n<pre><code class=\"language-LUA\">-- \u7b2c\u4e00\u4e2a\u53c2\u6570\u4ecd\u7136\u58f0\u660e\u4e3a\u5b9e\u4f8b\uff0c\u4f46\u8fd9\u6b21\u8c03\u7528\u8005\u4e0d\u9700\u8981\u4f20\nfunction localFun(caller, value)\n    print(caller.name, value)\nend\n\nlocal instance = {}\ninstance.name = \"some class\"\ninstance.fun = localFun\n\n-- \u91c7\u7528\u5192\u53f7\u8fdb\u884c\u8c03\u7528\n-- \u9ed8\u8ba4\u4f1a\u4f20\u4e00\u4e2a\u5b9e\u4f8b\uff08class\uff09\ninstance:fun(\"hello world\")<\/code><\/pre>\n<p>\u540c\u6837\u7684\uff0c\u5982\u679c function \u4e0d\u662f\u65e0\u4e3b\u7684\uff0c\u800c\u662f table \u7684\u4e00\u4e2a\u6210\u5458\uff0c\u90a3\u4e48 function \u4e5f\u53ef\u4ee5\u7701\u7565\u7b2c\u4e00\u4f4d\u53c2\u6570\u7684\u58f0\u660e\uff0c\u4f7f\u7528\u7f3a\u7701\u7684 self \u6307\u4ee3\u5b9e\u4f8b\uff08\u7c7b\u4f3c\u4e8e C# \u7684 self \u6307\u9488\uff09<\/p>\n<pre><code class=\"language-LUA\">local instance = {}\ninstance.name = \"some class\"\n\n-- \u65b9\u6cd5\u4e5f\u7528\u5192\u53f7\u7533\u660e\nfunction instance:fun(value)\n    print(self.name, value)\nend\n\ninstance:fun(\"hello world\")<\/code><\/pre>\n<p>\u4f46\u8fd9\u5e76\u4e0d\u610f\u5473\u7740\u8fd9\u4e2a function \u5c31\u662f\u79c1\u6709\u7684\u3001\u662f\u7ed1\u5b9a\u7684\uff0c<br \/>\n\u5b83\u968f\u65f6\u53ef\u80fd\u88ab\u522b\u4eba\u62ff\u53bb\u7528\uff0c\u751a\u81f3\u628a function \u5185\u90e8\u7528\u7684 self \u5077\u6362\u6210\u522b\u7684\u5b9e\u4f8b\uff08\u54ea\u6015\u8fd9\u4e0d\u662f\u8c03\u7528\u8005\u671f\u671b\u7684\uff09<\/p>\n<h2>\u95ed\u5305\u95ee\u9898<\/h2>\n<h3>\u95ed\u5305\u7684\u6982\u5ff5<\/h3>\n<p>\u95ed\u5305\u4f5c\u7528\u57df\u7684\u6982\u5ff5\u53ef\u4ee5\u53c2\u8003\uff1a<a href=\"https:\/\/www.lua.org\/pil\/6.1.html\">Programming in Lua : 6.1<\/a><\/p>\n<p>lua \u91cc\u7684 \u5168\u5c40\u53d8\u91cf\uff0c\u662f\u771f\u6b63\u610f\u4e49\u4e0a\u7684\u5168\u5c40\uff0c\u6240\u6709\u5728\u4e00\u4e2a environment\uff08lua\u865a\u62df\u673a\uff09\u91cc\u7684\u5185\u5bb9\u90fd\u5171\u4eab<br \/>\n\u800c\u5728 lua \u91cc\u4e5f\u6709\u4f5c\u7528\u57df\uff0c\u88ab\u79f0\u4e3a <code>lexical scoping\uff08\u8bed\u6cd5\u57df\uff09<\/code>\uff0c\u4f8b\u5982 function \u548c end \u7684\u8bed\u6cd5\u533a\u95f4\u5185<br \/>\n\u800c\u8fd9\u4e9b\u4f5c\u7528\u57df\u5185\u53ef\u4ee5\u4f7f\u7528\u4e0a\u5c42\u7684\u53d8\u91cf\uff0c\u79f0\u4e3a <code>external local variable(\u5916\u90e8\u5c40\u90e8\u53d8\u91cf)<\/code>\uff0c \u5728\u8fd9\u4e2a\u4e0a\u5c42\u4f5c\u7528\u57df\u7ed3\u675f\u4e4b\u524d\uff0c\u8fd9\u4e2a\u5916\u90e8\u5c40\u90e8\u53d8\u91cf\u90fd\u4f1a\u4fdd\u6301\u5b83\u7684\u503c\uff0c\u8fd9\u4f1a\u51fa\u73b0\u5728\u5f88\u591a\u573a\u5408\uff0c\u4f8b\u5982<\/p>\n<ul>\n<li>\u8fed\u4ee3\u5668<\/li>\n<li>\u6a21\u62df\u7c7b\u7684\u9759\u6001\u53d8\u91cf \u7b49\u7b49<\/li>\n<\/ul>\n<h3>\u59d4\u6258\u548cself\u4f20\u9012\u7684\u95ee\u9898<\/h3>\n<p>lua \u5e76\u6ca1\u6709 <code>.NET<\/code> \u90a3\u4e48\u5b8c\u5584\u3001\u7075\u6d3b\u7684\u673a\u5236\uff0c<br \/>\n\u5f53\u4f60\u5c1d\u8bd5\u5728 lua \u4e2d\u4f7f\u7528 delegate \u673a\u5236\u7684\u65f6\u5019\uff0c\u4f60\u7684 delegate \u5728\u7533\u660e\u7684\u65f6\u523b\u5c31\u7ed1\u5b9a\u4e86\u4e00\u4e2a \u5b9e\u4f8b\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-LUA\">local tool = {}\ntool.name = \"tool\"\nfunction tool:CallTool()\n    print(self.name)\nend\n\nlocal instance = {}\ninstance.name = \"instance\"\nfunction instance:CallInstance()\n    instance:callback()\nend\n\ninstance.callback = tool.CallTool\ninstance:CallInstance()\n\n-&gt; instance<\/code><\/pre>\n<p>\u8fd9\u79cd\u95ee\u9898\u7684\u5173\u952e\u5728\u4e8e\uff0c\u4e00\u5b9a\u8981\u7262\u8bb0 self \u53ea\u662f\u4e00\u4e2a\u8bed\u6cd5\u7cd6\uff0c\u5e76\u4e0d\u662f\u4ec0\u4e48\u5f88\u725b\u903c\u7684\u673a\u5236<br \/>\n\u4f60\u5728\u67d0\u4e2a table|module \u5185\u90e8\u7528 \u5192\u53f7 \u5b9a\u4e49\u4e86\u4e00\u4e2a function\uff0c\u53ea\u662f\u8bf4\u5b83\u9ed8\u8ba4\u662f\u548c\u8fd9\u4e2a table|module \u5173\u8054\u4e0a\u7684\uff0c\u4e0d\u4ee3\u8868\u5b83\u5b8c\u5168\u5f52\u5c5e\u4e8e\u8fd9\u4e2a\u5b9e\u4f8b<br \/>\n\u4efb\u4f55\u65f6\u5019\uff0c\u5176\u4ed6\u7684 table|module \u90fd\u53ef\u4ee5\u5c06\u5b83\u89c6\u4e3a \u7b2c\u4e00\u7c7b\u503c\uff0c\u628a\u5f15\u7528\u7ed9\u201c\u5077\u201d\u8fc7\u53bb\uff0c\u7136\u540e\u6362\u4e00\u4e2a self \u8fdb\u884c\u8c03\u7528<br \/>\n\u4e3a\u6b64\uff0c\u5728 lua \u4e2d\u7684 delegate \u5fc5\u987b\u6ce8\u660e\u8c03\u7528\u65f6\u7684\u5b9e\u4f8b\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u6709\u4eba\u79f0\u4e4b\u4e3a bind\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-LUA\">---\u95ed\u5305\u7ed1\u5b9a\nBind = function(self, func)\n    -- assert(self == nil or type(self) == \"table\")\n    assert(func ~= nil and type(func) == \"function\")\n    if self == nil then\n        return function(...)\n            return func(...)\n        end\n    else\n        return function(...)\n            return func(self, ...)\n        end\n    end\nend<\/code><\/pre>\n<h2>\u533f\u540d\u65b9\u6cd5<\/h2>\n<p>\u8fd9\u79cd\u672a\u547d\u540d\u7684\u65b9\u6cd5\u5b9a\u4e49\u88ab\u79f0\u4e3a\u533f\u540d\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-LUA\">local fun = function(...)\n    print(...)\nend<\/code><\/pre>\n<p>\u6b63\u5e38\u6765\u8bf4\u9700\u8981\u533a\u5206 <code>.<\/code> \u548c <code>:<\/code> \u7684\u8bed\u6cd5\u7cd6<\/p>\n<pre><code class=\"language-LUA\">local instance = {}\ninstance.name = \"instance\"\ninstance.fun = function(self, a)\n    print(self.name, a)\nend\n\ninstance.fun(instance, 1)\ninstance:fun(1)\n\n-&gt; instance        1\n-&gt; instance        1<\/code><\/pre>\n<p>\u7136\u800c\u5728 table \u6784\u9020\u65f6\u58f0\u660e\uff0c\u4f3c\u4e4e\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b<\/p>\n<pre><code class=\"language-LUA\">local instance = {\n    name = \"instance\",\n    fun1 = function(a)\n        print(a)\n    end,\n    fun2 = function(self, a)\n        print(self.name, a)\n    end\n}\n\ninstance.fun1(1)\ninstance:fun2(2)\n\n-&gt; 1\n-&gt; instance        2<\/code><\/pre>\n<p>\u533f\u540d\u65b9\u6cd5\u7684\u5b9a\u4e49\u7b49\u4ef7\u4e8e\u4f60\u521b\u5efa\u4e86\u4e00\u4e2a \u6307\u9488\uff08\u6216\u8005\u8bf4\u7b2c\u4e00\u7c7b\u503c\uff09<br \/>\n\u56e0\u6b64\u5728\u4e00\u4e9b\u9700\u8981callback\u7684\u573a\u5408\u4f60\u53ef\u4ee5\u76f4\u63a5\u4f20\u5165\u4e00\u4e2a\u533f\u540d\u65b9\u6cd5\uff08\u4f46\u8981\u6ce8\u610f \u95ed\u5305\u7ed1\u5b9aself\u7684\u95ee\u9898\uff09<br \/>\n\u5b98\u65b9\u628a\u8fd9\u79f0\u4e3a <code>higher-order\uff08\u9ad8\u9636\u8c03\u7528\uff09<\/code>\uff0c\u8fd9\u79cd\u7528\u6cd5\u53ea\u662f function \u4f5c\u4e3a\u7b2c\u4e00\u7c7b\u503c\u7684\u4e00\u79cd\u7075\u6d3b\u7528\u6cd5\uff0c\u5e76\u4e0d\u662f\u8fd9\u79cd\u503c\u7684\u7279\u6743<\/p>\n<h2>\u5d4c\u5957\u8c03\u7528\u7684\u7ec6\u8282<\/h2>\n<p><a href=\"https:\/\/www.lua.org\/pil\/6.3.html\">Programming in Lua : 6.3<\/a><br \/>\n\u8fd9\u7bc7\u6587\u7ae0\u6709\u63d0\u5230\uff0clua \u5728\u65b9\u6cd5\u5d4c\u5957\u65f6\u662f <code>proper tail<\/code> \u7684\u8c03\u7528\u673a\u5236\uff0c\u4f8b\u5982<\/p>\n<pre><code class=\"language-LUA\">function f(x)\n    return g(x)\nend<\/code><\/pre>\n<p>\u5f53 f \u8c03\u7528\u5b8c\u6210\u65f6\uff0c f \u7684\u8d44\u6e90\u5c31\u53ef\u4ee5\u91ca\u653e\u4e86\uff0c\u4e0d\u9700\u8981\u548c\u4e00\u4e9b\u8bed\u8a00\u4e00\u6837\u4fdd\u6301\u4e00\u4e2a\u65b9\u6cd5\u6808<br \/>\n\u8fd9\u4e5f\u76f4\u63a5\u4f7f\u5f97 lua \u4e0d\u9700\u8981\u9650\u5236\u9012\u5f52\u5c42\u6570\uff0c\u5e76\u4e14\u5728\u5355\u72ec\u7684 lua \u73af\u5883\u91cc\u4e0d\u4f1a\u56e0\u4e3a \u6b7b\u5faa\u73af\u3001\u6076\u6027\u9012\u5f52\uff08\u5e76\u975e\u771f\u6b63\u610f\u4e49\u4e0a\u7684\u9012\u5f52\uff09 \u5bfc\u81f4 <code>StackOverflow<\/code><sup id=\"fnref1:REF2\"><a href=\"#fn:REF2\" class=\"footnote-ref\">2<\/a><\/sup><\/p>\n<div class=\"footnotes\">\n<hr>\n<ol>\n<li id=\"fn:REF1\">SciTE \u9ed8\u8ba4\u7684 5.1\u7248\u672c\u662f\u6ca1\u6709\u7684\uff0c\u9700\u8981\u66f4\u9ad8\u7248\u672c&nbsp;<a href=\"#fnref1:REF1\" rev=\"footnote\" class=\"footnote-backref\">\u21a9<\/a><\/li>\n<li id=\"fn:REF2\"><a href=\"https:\/\/www.lua.org\/pil\/6.3.html\">https:\/\/www.lua.org\/pil\/6.3.html<\/a> : Because a proper tail call uses no stack space, there is no limit on the number of &#8220;nested&#8221; tail calls that a program can make. For instance, we can call the following function with any number as argument; it will never overflow the stack&nbsp;<a href=\"#fnref1:REF2\" rev=\"footnote\" class=\"footnote-backref\">\u21a9<\/a><\/li>\n<\/ol>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4ecb\u7ecd Lua \u4e2d function \u76f8\u5173\u7684\u4e00\u4e9b\u6982\u5ff5\u548c\u6280\u5de7<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[8],"_links":{"self":[{"href":"https:\/\/yanghanwen.cn\/index.php?rest_route=\/wp\/v2\/posts\/112"}],"collection":[{"href":"https:\/\/yanghanwen.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yanghanwen.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yanghanwen.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yanghanwen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=112"}],"version-history":[{"count":1,"href":"https:\/\/yanghanwen.cn\/index.php?rest_route=\/wp\/v2\/posts\/112\/revisions"}],"predecessor-version":[{"id":113,"href":"https:\/\/yanghanwen.cn\/index.php?rest_route=\/wp\/v2\/posts\/112\/revisions\/113"}],"wp:attachment":[{"href":"https:\/\/yanghanwen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yanghanwen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yanghanwen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}