Skip to content

a recreation of JavaScript class and prototype syntax

License

Notifications You must be signed in to change notification settings

ReRand/LuaClassJS

Repository files navigation

LuaClassJS

a recreation of JavaScript classes and prototypes in lua and luau

includes:

  • class, new, and extend keywords
  • class checking and super functions
  • isa and getclass built-in functions

local class, new, extend = require("classjs")("class", "new", "extend");


local Base = class "Base" {
  constructor = function(self, name)
    self.Name = name;
  end,

  AddAttribute = function(self, k, v)
    self.Attributes[k] = v;
    return self.Attributes;
  end,

  Attributes = {}
}


function Base.ClearAttributes(self)
  self.Attributes = {};
  return self.Attributes;
end


extend "Base" "User" {
  constructor = function(self, id, name)
    self.Id = id;
    self.__super(name);
  end,

  RemoveAttribute = function(self, k)
    self.Attributes[k] = nil;
  end,

  Attributes = {
    Type = "User"
  }
}


local user = new "User"("user_1", "User1");

print(user.Attributes); -- { Type = "User" }

user.RemoveAttribute("Type");
user.AddAttribute("Status", "Cool");

print(user.Attributes); -- { Status = "Cool" }

user.ClearAttributes();

print(user.Attributes); -- { }

About

a recreation of JavaScript class and prototype syntax

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages