Working with classes

A class is a type of physical or unintelligible living or non-living thing having properties and behaviors - commonly known as "methods" -.

jOOPL has full-featured classes as many other major languagues like C# or Java, and concentrates its capabilities through both $class, $def and $new keywords (learn more about keywords here).

There are some recommended readings before going on on working with classes:

1. The $def and $new keywords: designing classes

The most important keywords when working with classes are $def and $new.

The $def keywords is a constructor designed to define/declare classes. $def accepts a set of parameters as an object map (this would be a sample object map - actually an anonymous object -{ param1: "value", param2: "other value"}).

In the next table are specified the supported parameters by the object map argument of $def:

Parameter name Description
$constructor A function defining the class constructor. Only one constructor is supported.
$members An object map containing accessors (getters/setters) and method for the defining class
$extends A class defined using jOOPL representing the base class. This parameter is optional.
$implements An array of one or more interfaces defined using jOOPL that must be implemented by the class.

A sample class definition/declaration

$namespace.register("joopl.Samples", function () {
    this.SomeClass = $def({
        $constructor: function (args) {
        },
        $members: {
            SomeMethod: function (yourName) {
                alert("Hello world, " + yourName + "!");
            }
        }
    });
});

A sample class definition/declaration with base class and implementing an interface

$namespace.register("joopl.Samples", function() {

    this.SomeClass = $def({
        $constructor: function (args) {
        },
        $members: {
            SomeMethod: function (yourName) {
                alert("Hello world, " + yourName + "!");
            }
        },
        $extends: this.SomeOtherClass,
        $implements: [this.ISomeInterface]
    });
}

A sample class definition/declaration with a read-write property

$namespace.register("joopl.Samples", function () {

    this.SomeClass = $def({
        $constructor: function (args) {
            this.$_.name = args.name;
        },
        $members: {
            get_Name: function () { return this.$_.name; },
            set_Name: function (value) { this.$_.name = value; },

            SomeMethod: function () {
                // "name" is a read-write property
                alert("Hello world, " + this.name + "!");
            }
        },
        $extends: this.SomeOtherClass,
        $implements: [this.ISomeInterface]
    });
});

2. Creating instances of classes

Once a class is defined/declared, the obvious step is instantiating it. This is achieved using the $new keyword. This keyword is a constructor having two parameters:

Parameter name Description
clazz The class defined/declared using jOOPL and the $def keyword
args An object map containing an arbitrary number of arguments required by the class constructor

For example, having a class SomeClass with a constructor expecting a person name would be instantiated like the next code sample:
// Store the newly created instance in a variable!
$namespace.register("joopl.Samples", function () {
    var instanceofSomeClass = new this.SomeClass({ name: "Matias" });
});

Last edited Apr 6, 2013 at 12:38 PM by MFidemraizer, version 35

Comments

No comments yet.