2012. december 1., szombat

Protain - nodes, concept

In this article, I would like to introduce the meaning of node in Protain.

But first, let's talk about a different approach to object-oriented programming. I named it "prototype-oriented" programming but only for myself. The first thing I would like you to forget is the idea of class, in the classical meaning. Instead, think about objects only. Of corse we are still in JavaScript!

What is a node?

In JavaScript, in prototypal-inheritance, the prototype will tell you where the instance is derived from, through the instance's prototype. Which is a reference to another object. And object properties can refer to other object's properties. That is what a node is: an object, with specified properties.

For cubes: "A node is an object's attribute collection, used by one or more classes. It's purpose is to share the same functionality between classes instead of specifying them again and again."

For example:
Suppose you have 5 different classes in the same namespace and a bunch of nodes (functionalities). You can "connect" these nodes by specifying the 5 classes. You can connect these classes differently.
Also, it is very handful when you don't have to re-model, re-design and re-implement the whole prototype-chain when you have to extend the list of functionalities.

Let's define two nodes. One in the current context-namespace and the other in the hu.benus.global namespace. We will use the protain.node method, which takes the following arguments:
  • namespace - the namespace to register the node to
  • name - the name of the node
  • object - the node as an object
protain.namespace('hu.benqus.namespace_tutorial', function (path) {
    /**
     * creating a node (functionality)
     * called 'name' containing 2 methods
     * on the current namespace
     */
    protain
        .node(this, 'name', {
            /**
             * @return {String}
             */
            getName: function () {
                return this.name;
            },
            /**
             * @param name {String}
             */
            setName: function (name) {
                this.name = name;
                return this;
            }
        });

    /**
     * creating another namespace
     * @type {Object}
     */
    var global = protain.namespace('hu.benqus.global');

    /**
     * creating a node called 'guid'
     * on the hu.benqus.global namespace
     */
    protain
        .node(global, 'guid', function () {
            /**
             * static property
             * @type {Number}
             */
            var id = 0;

            return {
                /**
                 * specifies an ID attribute for the context
                 */
                generateId : function () {
                    this.ID = id++;
                    return this;
                }
            }
        }());
});
Now, you can define as many classes as you want using the same name and guid nodes (functionalities).

Dealing with nodes is simple again, just specify the context, the name, and the node object and you are good to go.

Next one will be the class (prototype object) creation. Stay tuned for more! =)

Find Protain on GitHub: https://github.com/benqus/protain

Nincsenek megjegyzések:

Megjegyzés küldése