尝试调用类方法时出错:尝试索引本地'self'(零值) - Lua

Fro*_*rog 2 lua texas-instruments

我正在创建一个应该在TI-Nspire计算器上运行的lua脚本.问题是,在运行我的脚本时,我在调用方法Attempt to index local 'self' (a nil value)时遇到错误button:activate().解析器说错误在下面的代码中的第8行.有问题的代码如下:

button = class(view)

function button:init()
    self.selected = false
end

function button:activate()
    self.selected = true
end
Run Code Online (Sandbox Code Playgroud)

我像这样调用activate函数:

item = button()
local action = "activate"
local arguments = {}
item[action](unpack(arguments))
Run Code Online (Sandbox Code Playgroud)

我知道这个class()函数在"stock"Lua中不存在,它是TI-Nspire Lua实现中可用的函数.您可以在此处找到其定义和用法.

lhf*_*lhf 6

obj:methodname(args)是糖obj.methodname(obj,args).因此,如果要使用语法item[action](unpack(arguments)),则需要使用item[action](item,unpack(arguments)).否则,请尝试item:activate(unpack(arguments))显式使用方法.