Simple object declaration
myObjects = class { // class attributes aNumber = 4; aString = "some text"; __new = function() { // instance attributes this.anotherNumber = 10; }; addMax = function(p1, p2) { if (p1 > p2) return p1 + self.aNumber; else return p2 + this.anotherNumber; }; }; o = myObject.new(); i = o.addMax(1, 2); // 12 j = o.addMax(7, 5); // 11
Inheritance
It is possible for a class to inherit from another class, to get all its characteristics (attributes and methods) with the possibility to override them.
Animal = class { walk = function() { /* some code */ }; }; Dog = class extends Animal { run = function() { /* ... */ }; }; Snoopy = class extends Dog { walk = function() { /* ... */ }; }; snoop = new Snoopy(); snoop.run(); // from Dog snoop.walk(); // from Snoopy, not from Animal
Avoiding inheritance
To prevent sub-classes of a given class, mark it as final.
Animal = final class { /* ... */ }; Doc = class extends Animal { /* ... */ }; // ERROR
Avoiding attributes overloading
To avoid the overloading of an attribute, mark it as final.
Animal = class { final walk = function() { /* ... */ }; }; Dog = class extends Animal { walk = function() { /* ... */ }; // ERROR };