找到你要的答案

Q:self index becomes argument

Q:自索引变参数

Given is this object:

foo = { }

function foo:new( p1, p2 )
  local object = {}
  setmetatable( object, self )
  self.__index = self
  object .p1 = p1
  object .p2 = p2
  return object
end

function foo:bar( boolbar )
  self.p1 = not boolbar
  print( self.p1 )
end

Now I got another object, which is calling foos function by using a reference to foo ( foo is not accessable directly ) and passing an argument to it. Somehow, self becomes the argument.

reference_to_foo = foo 

function the_other_object:some_func()
  -- does some stuff
  reference_to_foo:bar( true )
end

Calling the foo:bar() works well, but per reference_to_foo the index self becomes the argument (boolean in this case).

In :bar(), I'll get this error:

attempt to index local 'self' (a boolean value)

Why is that and how can I solve this?

给出了这个对象:

foo = { }

function foo:new( p1, p2 )
  local object = {}
  setmetatable( object, self )
  self.__index = self
  object .p1 = p1
  object .p2 = p2
  return object
end

function foo:bar( boolbar )
  self.p1 = not boolbar
  print( self.p1 )
end

现在我有另一个对象,这是通过使用一个参考foo调用粮食功能(Foo不是可以直接)和传递一个参数给它。不知何故,自我成为争论。

reference_to_foo = foo 

function the_other_object:some_func()
  -- does some stuff
  reference_to_foo:bar( true )
end

调用foo:bar()效果很好,但每reference_to_foo指数自变参数(布尔在这种情况下)。

在bar(),我会得到这个错误:

尝试索引本地“自我”(布尔值)

为什么会这样,我该怎么解决呢?

object  lua