Get the ancestor of an object or instance.

The super() function permits you to access ancestor methods, fields and properties.



An object (as defined by the Object() function) or an instance.

Return value

This function returns the ancestor object, or nil if no ancestor is found.
If you want to call ancestor methods with the super() function, you will need to use the "." dot notation to specify the self instance explicitly
Method call using ":" semicolon is not possible as super() returns an Object, not an instance value.


Polygon = Object {} -- Defines a new Object Polygon -- Defines a constructor for the Polygon Object function Polygon:constructor(n) -- Defines a new filed 'sides' self.sides = n end Square = Object (Polygon) -- Defines a new Object Square, inheriting from Polygon -- Defines a constructor for the Square Object (with 4 sides) function Square:constructor() -- Don't rewrite previous code, just use super() to call the Polygon constructor -- to initialize the sides field with the value of 4 super(self).constructor(self, 4) end -- Should print true print(super(Square) == Polygon) -- Should print 4 print(Square().sides)