【问题标题】:function of object in nested table syntax?嵌套表语法中对象的函数?
【发布时间】:2014-12-24 22:39:57
【问题描述】:

我有一张这样的桌子给我

'(' 预期在 'errorline' 的 't' 附近

这意味着一定有语法错误,但我无法检测到。你知道语法有什么问题吗?

t = {}

t[x] = {
   some = "data",

   foo = function() return "bar" end,

   elements = {   -- the class is working 100%, have used it for several projects.
     mon =  class:new(param), 
     tue =  class:new(param2),
     n   =  class:new(param3),
   },

   function t[x].elements.mon:clicked()   -- <<< --- ERRORLINE
      --dosomething
   end,
}

【问题讨论】:

    标签: class syntax lua lua-table


    【解决方案1】:

    在表格声明之后添加函数 t[x].elements.mon:clicked() 即在表格的右大括号之后。

    t = {}
    
    t[x] = {
       some = "data",
    
       foo = function() return "bar" end,
    
       elements = {   -- the class is working 100%, have used it for several projects.
         mon =  class:new(param), 
         tue =  class:new(param2),
         n   =  class:new(param3),
       }
    }
    
    
    t[x].elements.mon.clicked = function(self)
          --dosomething
    end
    

    编辑:

    如 cmets 函数中所述,t[x].elements.mon:clicked() 不起作用。 函数声明应该是t[x].elements.mon.clicked = function(self)

    请注意,如果您使用冒号调用点函数,则该函数的第一个参数将是 self。即,如果您将该函数称为c = t[x].elements.mon:clicked(a,b),则该函数应该是 t[x].elements.mon.clicked = function(self,a,b)

    【讨论】:

    • mhhh,肯定是更好的风格,但给了我 '(' 预期在 '['好奇
    • 你不能在那个函数语法中做括号索引;你需要走很长的路:t[x].elements.mon.clicked = function(self) ... end
    猜你喜欢
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 2020-06-29
    • 1970-01-01
    • 2013-10-02
    • 2016-05-18
    • 1970-01-01
    • 2018-04-02
    相关资源
    最近更新 更多