Classes & Object-Oriented Programming
REK supports class-based object-oriented programming. Classes act as blueprints for creating objects with shared methods and state.
Defining a Class
Use the class keyword. The init method is the constructor.
class User {
fn init(name, email) {
this.name = name
this.email = email
this.active = true
}
fn describe() {
return this.name + " (" + this.email + ")"
}
}
Creating Instances
Use the new keyword to create an instance of a class.
let alice = new User("Alice", "alice@example.com")
log(alice.describe())
The this Keyword
Inside methods, this refers to the current instance. You can use it to access or modify properties.
Inheritance
REK supports single inheritance using the < symbol.
class Admin < User {
fn init(name, email, level) {
super.init(name, email)
this.level = level
}
fn deleteUser(user) {
log("Deleting " + user.name)
}
}
let admin = new Admin("Bob", "bob@admin.com", "super")
admin.describe() // Inherited method
admin.deleteUser(alice)
Note: REK uses a prototype-based approach under the hood, similar to other dynamic languages, but exposes a familiar class syntax.