LuaClass - A JavaScript approach to classes [Archive]

What is LuaClass?

LuaClass is an implementation of classes from JavaScript (to an extent). This does not include private variables due to Lua’s odd environment.

Documentation
Documentation has been removed as this project is no longer supported and deleted. The Wally installation method still works in case if you’re interested; however, if you do want the source code for yourself, you will have to get it from the last commit that it was present in my packages (d0fb17d).

Example

local new, class, extends, meta = require(game.ReplicatedStorage.Packages.LuaClass)()

class "Animal" {
    constructor = function(this, name: string)
        this.name = name
        return this
    end,

    speak = function(this)
        print(this.name .. " makes a noise.")
    end,
}

local animal = new "Animal" ("Sprok")
animal.speak() -- "Sprok makes a noise."

class "Dog" [extends "Animal"] {
    speak = function(this)
        print(this.name .. " barks.")
    end,

    growl = function(this)
        print(this.name .. " growls.")
    end,
}

local dog = new "Dog" ("Mitzie")
dog.speak() -- "Mitzie barks."
dog.growl() -- "Mitzie growls."

Installing LuaClass

LuaClass is available via Wally and expects you to be using a Rojo workflow. If you rather download it from GitHub you can get the .zip file from it’s source.

Using Wally

[dependencies]
LuaClass = "alexinite/lua-class@0.1.2"

Comparisons

If you’ve never used Metatables to create sudo-classes, here’s an example of what that looks like versus the Animal and Dog classes I made earlier.

local Animal = {}
Animal.__index = Animal

function Animal.new(name: string)
    local mt = {}
    mt.name = name
    setmetatable(mt, Animal)

    return mt
end

function Animal:speak()
    print(self.name .. " makes a noise.")
end

local animal = Animal.new("Sprok")
animal:speak() -- "Sprok makes a noise."

local Dog = {}

function Dog:__index(index)
    return Dog[index] or Animal[index]
end

function Dog.new(name: string)
    local mt = {}
    mt.name = name
    setmetatable(mt, Dog)

    return mt
end

function Dog:speak()
    print(self.name .. " barks.")
end

function Dog:growl()
    print(self.name .. " growls.")
end

local dog = Dog.new("Mitzie")

dog:speak() -- "Mitzie barks."
dog:growl() -- "Mitzie growls."
and in TypeScript...
class Animal {
    name: string

    constructor(name: string) {
        this.name = name;
    }

    speak() {
        console.log(this.name + " makes a noise.")
    }
}

const animal = new Animal("Sprok");
animal.speak(); // "Sprok makes a noise."

class Dog extends Animal {
    constructor(name: string) {
        super(name);
    }

    speak() {
        console.log(this.name + " barks.");
    }

    growl() {
        console.log(this.name + " growls.");
    }
}

const dog = new Dog("Mitzie");
dog.speak(); // "Mitzie barks."
dog.growl(); // "Mitzie growls."

Metamethods

Whenever you’re creating a class in Lua, it might just be more practical to continue to use metamethods: this is still fully possible!

local new, class, extends, meta = require(game.ReplicatedStorage.Package.LuaClass)()

class "Animal" {
    constructor = function(this, name: string)
        this.name = name
        return this
    end,

    [meta "index"] = function(this, index)
        return this.name .. " " .. index .. "s."
    end,
}

local animal = new "Animal" ("Mitzie")
print(animal["bark"]) -- "Mitzie barks."

Documentation

10 Likes

I thought it was this, correct me if im wrong

{
amogus : function()

end
}

edit : nvm, you couldve made it more clear by your title that its typescript not javascripr

3 Likes

JavaScript and TypeScript do classes the same way. ( + )

The only reason I used TypeScript in the examples is that it’s typed.

1 Like