Class properties / accessors

Accessors are a special methods providing an encapsulated access to a private class field.

In jOOPL the convention is:
  • All private fields' identifiers starts with an underscore and a camel-cased identifier.
  • A private field must not be directly accessed: always use an accessor.
  • The method that gets field's value, starts with "get_" and after that some pascal or camel-cased identifier.
  • The method that sets field's value, starts with "set_" and after that some pascal or camel-cased identifier.
    • Remarks: Setter accessor methods must have an input parameter called value.

This would a sample:

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

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

Following these conventions, starting with 2.0 version of jOOPL, the infrastructure will create actual JavaScript ECMA-Script 5 and above accessors since it is using the standard Object.defineProperty(...) method under the hood.

Above statement means that the whole class with get/set accessors, once instantiated, will contain both the get_Name()/set_Name(...) pair and a single read-write property name. The read-write property accessors will use the ones defined in the class with get_ and/or set_.

This means that properties are both callable using the get_ and set_ methods and the actual ECMA-Script 5 property!

Read-only properties

Read-only properties are those defined with only a get_ method. For example:

$namespace.register("jOOPL.Samples", function () {
    this.A = $def({
        $constructor: function (args) {
            this.$_.name = args.name;
        },
        $members: {
            get_Name: function () { return this.$_.name; }
        }
    });
});

Once A gets instantiated, the object will have both a get_Name method and a read-only property name:
$namespace.register("jOOPL.Samples.A", function() {
    var instance = this.A({ name: "Matias" });

    var name = instance.get_Name(); // Valid!
    name = instance.name; // Valid too!
    instance.name = "John"; // NO! Error! name is a read-only property!
});

Write-only properties

In opposite to read-only properties, write-only properties are those defined with only a set_ method.

For example:

$namespace.register("jOOPL.Samples", function () {
    this.A = $def({
        $constructor: function (args) {
            this.$_.name = args.name;
        },
        $members: {
            set_Name: function () { return this.$_.name; }
        }
    });
});

Instantiating A will build the whole write-only property:
$namespace.register("jOOPL.Samples", function () {
    var instance = new this.A({ name: "Matias" });

    var name = instance.get_Name(); // Error! get_Name is undefined! It is a write-only property!
    name = instance.name; // Error! name does not have any getter!
    instance.set_Name("John"); // OK! "A" class has a setter accessor!
    instance.name = "John"; // OK! name has a setter as it is a write-only property.
});

Last edited Apr 6, 2013 at 12:47 PM by MFidemraizer, version 13

Comments

No comments yet.