Skip to content

Prototypes

Rohan Singh edited this page Feb 23, 2015 · 5 revisions

Mond supports prototype-based inheritance which means an object can inherit the values from another object. This only affects value lookups with the x.y and x[y] operators.

The prototype value of an object can be accessed with Value.getPrototype and can be changed with Object.setPrototype.

Prototypes can be used to implement class-like object-oriented programming:

// constructor function
fun BaseClass() {
    return {
        getNumber: fun () -> 10,
        add: fun (x, y) -> x + y
    };
}

// constructor function
fun Class() {
    var base, this = {
        getNumber: fun () -> this.add(base.getNumber(), 5)
    };

    base = BaseClass();
    this.setPrototype(base);

    return this;
}

var instance = Class();
return instance.getNumber();