Classes and objects
Bundle data and behavior together, with a constructor and methods you can chain.
A class bundles data with the functions that work on it. You create an object from a class with new.
A first class
class Counter {
function init( start = 0 ) {
variables.count = start;
return this;
}
function increment() {
variables.count = variables.count + 1;
return this;
}
function get() {
return variables.count;
}
}
c = new Counter( 5 );
c.increment();
c.increment();
println( c.get() );
Output:
7
initis the constructor. It runs when you callnew Counter( 5 ).variables.countis private state, stored inside the object. Code outside the class reaches it only through the methods.thisis the object itself.
Chaining
Because increment returns this, calls can be chained:
class Counter {
function init( start = 0 ) {
variables.count = start;
return this;
}
function increment() {
variables.count = variables.count + 1;
return this;
}
function get() {
return variables.count;
}
}
println( new Counter().increment().increment().increment().get() );
Output:
3
Public data
Values you store on this are public. That is simple, but it means anyone can change them.
class Account {
function init( owner, balance = 0 ) {
this.owner = owner;
variables.balance = balance;
return this;
}
function deposit( amount ) {
variables.balance = variables.balance + amount;
return this;
}
function withdraw( amount ) {
if ( amount > variables.balance ) {
throw( type = "Funds", message = "Insufficient funds" );
}
variables.balance = variables.balance - amount;
return this;
}
function getBalance() {
return variables.balance;
}
}
account = new Account( "Ada", 100 );
account.deposit( 50 ).withdraw( 30 );
println( account.owner & " has " & account.getBalance() );
try {
account.withdraw( 500 );
} catch ( Funds e ) {
println( "Refused: " & e.message );
}
Output:
Ada has 120
Refused: Insufficient funds
Here the owner is public on this, while the balance is private, so the only way to change it is through deposit and withdraw, which enforce the rules. The try and catch at the bottom are the subject of the next lesson.
A class that holds a collection
class Stack {
function init() {
variables.items = [];
return this;
}
function push( value ) {
variables.items.append( value );
return this;
}
function pop() {
return variables.items.pop();
}
function size() {
return variables.items.len();
}
}
s = new Stack().push( 1 ).push( 2 ).push( 3 );
println( s.pop() );
println( s.size() );
Output:
3
2
Going further
Explore inheritance, interfaces and properties in the class reference.
Try it
Add a reset() method to Counter that sets the count back to zero and returns this.