17.4.09

Anonymous Construct Pattern

I've been furiously working on "the red pill"... this is the latest

JavaScript. A powerful language, misunderstood.

So many people have tried to tame it with libraries. Many have failed, some have succeeded.

The idea behind a library either local, company-wide or open source, is to abstract imperfect parts: Not all browsers behave the same and it's extremely difficult to tame them. Gathering all this knowledge into a library saves a lot of time.

Many have tried to put some structure into JavaScript, but have only succeeded in creating only Java-ish. Classes, supers, the works.

A good abstraction is one that we don't have to think about. It just works and it's easy to use it.

Let's say that we want to make the powerful in JavaScript more friendly for kids that shouldn't play with fire. Let's tame closures for them and offer the scopes they are familiar with: private and public. Classical inheritance is sack of potatoes: heavy (when full of potatoes) and old school.

Let's say we create a translator for the rest.

Part I: Construct


  • An anonymous construct does not pollute; it is humble. Only its UID is exposed.

    Construct's instance and static (immutable) scaffolds:

    /**
    * @constructor
    */
    (window.Construct = function(){
    // instance
    }).prototype = new function(){
    // static
    };

    Private members, public members:

    (window.Construct = function(){
    this.private = {};
    }).prototype = new function(){
    this.public = {};
    };

    Closures:

    (window.Construct = function(){
    var closure = {};
    this.private = function(){
    with(closure){
    // private, with access to closure objects
    }
    };
    }).prototype = new function(){
    var closure = {};
    this.public = function(){
    with(closure){
    // public, with access to closure objects
    }
    };
    };
  • No comments: