lux.prototype a LUX prototype

A prototype-based implementation for object oriented programming. It returns the root prototype object, with which you can create new objects using prototype:new. Objects created this way automatically inherit fields and methods from their parent, and may override them. It is not possible to inherit from multiple objects.

Functions

prototype:new (object)

Creates a new object from a prototype. If the self object has a __construct field as a function, it will be applied to the new object. If it has an __init field as a table, its contents will be cloned into the new object.

Parameters:

  • any object

    A table containing the object’s fields.

Usage:

    object = prototype:new { x = 42, text = "cheese" }

prototype:__super ()

Returns the parent of an object. Note that this may get confusing if you use prototypes as classes, because obj:__super() will most likely return the object’s class, not its class' parent class. In this case, it is better and more explicit to use Class:__super() directly.

Returns:

    any

    The object’s parent.

prototype:__bind (method_name)

Binds a method call. This is just an auxiliary method. Specially useful when you need to provide a callback function as a method from a specific object.

Parameters:

  • any method_name

    The name of the method being bound.

Returns:

    any

    A function binding the given method to the object.

Usage:

      local object = prototype:new { x = 42 }
    
      function object:get()
        return x
      end
    
      local get1 = object:new{}:__bind 'get'
      local get2 = object:new{ x = 1337 }:__bind 'get'
    
      -- Will print "42 1337"
      print(get1(), get2())

prototype:clone ()

Clones the object. Not to be confused with prototype:new. This recursevely clones an object and its fields. It may go into an infinite loop if there is a cyclic reference inside the object. This function may also be used to clone arbitrary Lua tables.

Returns:

    any

    A clone of this object.