如何在初始化期间自引用表。

有更短的方法来做这个吗:

local thisismytable = {
    non = sequitur
}
thisismytable.whatismytable = thisismytable

感谢任何帮助。 我不想重新创建已经存在的功能。

点赞
用户2226988
用户2226988

不行。

如果你能忍受这两个表达式之间的区别 thisismytable:whatismytable() 而不是 thisismytable.whatismytable,那么可以这样做:

local thisismytable = {
    non = sequitur,
    whatismytable = function (self) return self end
}

测试:

print(thisismytable)
print(thisismytable:whatismytable())

更多用法:

print(thisismytable:whatismytable().non)
2015-09-12 14:21:48
用户2282701
用户2282701

你不能。我使用了一个辅助函数。

local function ref(t)
  for k, v in next, t do
    if v == ref then t[k] = t end
  end
  return t
end

local root = ref{left=ref, right=ref}
assert(root.left == root)
2015-09-12 16:53:56