gpt4 book ai didi

lua - [函数类 :me() end] equal to [function class. me(self) end] 在 lua 中吗?

转载 作者:行者123 更新时间:2023-12-01 12:37:07 25 4
gpt4 key购买 nike

我很难理解 lua 中的冒号运算符。所以我对冒号运算符做了一些实验并得出了这个结论。

class = {}

function class:me()
end

等于

function class.me(self)
end

我的结论是否正确?

如果不是,是什么问题?

最佳答案

你的结论是正确的。

: 形式只是 .以 self 作为第一个参数的形式。这是记录在案的:

The colon syntax is used for defining methods, that is, functions that have an implicit extra parameter self. Thus, the statement

 function t.a.b.c:f (params) body end

is syntactic sugar for

 t.a.b.c.f = function (self, params) body end

关于lua - [函数类 :me() end] equal to [function class. me(self) end] 在 lua 中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28684784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com