Why it is impossible to obtain the number of array elements?

I'm an Amateur, please do not throw Slippers, but there is a problem
I created a certain "class" to which I add items, and when I try to read the number of elements that are present in the array using "#" I 0

class = {}
function class:new()

 local class = {}
 class.layer = {}

 function class:add(object)
 for key, value in pairs(object) do
 self.layer[key] = value

 function class:returnCount()
 return #self.layer --Returns 0

 setmetatable(class, self)
 self.__index = self; return class
June 3rd 19 at 19:37
1 answer
June 3rd 19 at 19:39
# can not read the key-value pairs.
And how else to learn? - Karli_Windler commented on June 3rd 19 at 19:42
There are 2 options:
1. Add a variable to the class and increase decrease its adding removal.
2. Add function counting number of elements about this method
cnt = 0
for _,_ in pairs(a) do
 cnt = cnt +1
- Darron.Trembl commented on June 3rd 19 at 19:45

Find more questions by tags Lua