相关疑难解决方法(0)

混淆使用"." 在Lua中使用__index和命名空间表示法

我对使用"."的以下两种语法感到困惑.

  1. 根据我的理解,__index当一个键不存在于表中但存在于其元表中时调用.那么为什么列表会调用__index然后将其自身分配给list.__index

    list = {}
    list.__index = list
    
    setmetatable(list, { __call = function(_, ...)
    local t = setmetatable({length = 0}, list)
      for _, v in ipairs{...} do t:push(v) end
      return t
    end })
    
    function list:push(t)
      if self.last then
        self.last._next = t
        t._prev = self.last
        self.last = t
      else
       self.first = t
       self.last = t
      end
      self.length = self.length + 1
    end 
      .
      .
      .
    local l = list({ 2 }, {3}, {4}, { 5 …
    Run Code Online (Sandbox Code Playgroud)

lua namespaces metatable meta-method

2
推荐指数
1
解决办法
319
查看次数

标签 统计

lua ×1

meta-method ×1

metatable ×1

namespaces ×1