Skip to content

Latest commit

 

History

History
64 lines (48 loc) · 1.44 KB

README.md

File metadata and controls

64 lines (48 loc) · 1.44 KB

lua-class

a class module for lua using similar syntax to other languages

Getting Started

Require the module, call it, then assign it two variables, class and new.

local class, new = require("class")()

Declaring Classes

To declare a class, you use the class function.

class "phone" {}

Data can be added to classes easily.

class "phone" {
    price = 500
}

Creating Objects

You can create objects using the new function.

class "phone" {
    price = 500
}

local iphone = new "phone"

You can now access and modify the object without it affecting the original class.

class "phone" {
    price = 500
}

local iphone = new "phone"
iphone.price = 1000

print(iphone.price) -- output: 1000

Class Constructors

Constructors are a special property of classes. They are a function which gets called whenever an object of the class is called. You can assign arguments to them which will be assigned when an object of the class is called.
The self keyword can be used inside of the constructor as well as every other function in the class to access the current object. ( i forgot to add it to every function, will be added very soon. )

class "phone" {
    price = 500,
    constructor = function(starting_price)
        self.price = starting_price
    end
}

local iphone = new "phone"(1000) -- calls constructor function with given arguments
print(iphone.price) -- output: 1000