Reputation: 45
I was just wondering why this returned Mexico not England? I've been trying to separate x1 to x6 by their properties, like continent, but I haven't made much progress.
world = {continent = "", country = ""}
function world:new (o,continent,country)
o = o or {}
setmetatable(o, self)
self.__index = self
self.continent= continent or ""
self.country = country or "";
return o
end
x1 = world:new(nil,"Europe","England")
x2 = world:new(nil,"Europe","France")
x3 = world:new(nil,"Africa","Algeria")
x4 = world:new(nil,"Africa","Nigera")
x5 = world:new(nil,"America","United States")
x6 = world:new(nil,"America","Mexico")
list_1 = {x1,x2,x3,x4,x5,x6}
print(list_1[1].country)
Upvotes: 1
Views: 63
Reputation: 811
In the context of new
, self
is the metatable, and o is the object. Setting attributes on self
overwrites the previous values. So, change
self.continent= continent or ""
self.country = country or ""
to
o.continent= continent or ""
o.country = country or ""
Upvotes: 4