Introduction

When we speak of metadata, what we're really talking about is configuration of a class or a member of a class. Think of visibility, whether an implementation is final, abstract, immutable or even nullable. Metadata can be applied to class members by declaring them as a string.

Properties will have getter- and setter methods generated automatically if they are declared public or protected. The setter automatically checks if the type of the passed argument matches the type of the property as it is defined in the declaration.

For example:

var MyClass = Class({
    // visibility
    // |     flags
    // |       |     type
    // |       |       |    name
    // |       |       |     |
    'public nullable string name': null,

    //            function signature      function signature
    //            (parameter types)      (parameter variables)
    //                    |                       |
    'public method(string, number)': function(str, num) {
        this.name = "";
        for (var i = 0; i < num); i++) {
            this.name += str + " ";
        }
    };
});

var my_class = new MyClass();

// Set the 'name' property to "Bob".
my_class.setName('Bob');

// Reset it to null.
my_class.setName(null);

// Throws exception: "setName expects string, number given."
my_class.setName(123);

// calls a method
my_class.method('Joe', 3);

// Get the name (returns: "Joe Joe Joe ")
my_class.getName();

// Throws exception: "Couldn't find a function handler to match: method(string, string)."
my_class.method('Joe', 'Bob');

The order in which visibility, flags and types are defined does not matter as long as the name of the property always comes last.

Accepted metadata:

Visibility

  • public
  • protected
  • private

Read more about this in the visibility section.

Flags

  • nullable
  • immutable
  • abstract
  • final

Types

  • any
  • string
  • number
  • boolean
  • object
  • function
  • custom

Read more about this in the types section.