Start pagina

XJS Library


Blog lezen

A JavaScript library to make OO programming less verbose.

You can also download the file (you might need to choose "Save link as...", depending on your browser).

For a example of how to use this, see the chatbox code.


/*
    Copyright (c) 2005 Joost Diepenmaat, Zeekat Softwareontwikkeling, 
    all rights reserved.

    Permission is hereby granted, free of charge, to any person obtaining 
    a copy of this software and associated documentation files (the 
    "Software"), to deal in the Software without restriction, including 
    without limitation the rights to use, copy, modify, merge, publish, 
    distribute, sublicense, and/or sell copies of the Software, and to 
    permit persons to whom the Software is furnished to do so, subject to 
    the following conditions:

    The above copyright notice and this permission notice shall be included
    in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
    OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
    OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


    This is the xjs librarary $Revision: 1.5 $

    It provides some basic support for namespaces and OO programming
    because core javascript is just too verbose to do everything
    by hand.

    The webpage for this code is at http://zeekat.nl/downloads/xjs/
    
*/

// all functions in this file are created in the
// nl.zeekat.xjs namespace

var nl = nl ? nl : {};
nl.zeekat = nl.zeekat ? nl.zeekat : {};
nl.zeekat.xjs = nl.zeekat.xjs ? nl.zeekat.xjs : {};

    /*
        the global object is available as nl.zeekat.xjs.global
    */

nl.zeekat.xjs.global = this;


(function() {
    var xjs = nl.zeekat.xjs;

    /* 
        this function creates a global namespace given as a 
        "top.sub.sub2" string. intermediate objects are kept
        returns the end object ("sub2" in the example)
    */
    
    xjs.namespace = function (name) {
        var identifiers = name.split(".");
        var top = xjs.global;
        for (var i =0; i < identifiers.length; i++) {
            if (! top[identifiers[i]]) {
                top[identifiers[i]] = {};
            }
            top = top[identifiers[i]];
        }
        return top;
    };


    /*
        this function returns a constructor function
        the argument is a single object. all properties
        of the argument are copied to the prototype of the 
        constructor.
    
        special properties:
        
        prototype:  
            if a prototype property is specified
            it will be used as the prototype for the constructor.
            any additional properties will be added to that
            prototype.
            
            the prototype's constructor.prototype will be available as
            constructor.superclass. this means you can do
            
            Constructor.superclass.method.apply(this, arguments)
            
        init:
            if specified, this should be a method used to
            initialize the object. any arguments passed to the
            constructor will be passed to the init() method
            *after* the superclass's constructor is called.
    */

    xjs.constructor = function (opts) {
        var constructor = function () {
            if (constructor.superclass) {
                constructor.superclass.constructor.apply(this, arguments);
            }
            if (constructor.prototype.init) {
                constructor.prototype.init.apply(this,arguments);
            }
        };
        if (opts.prototype) {
            constructor.prototype = opts.prototype;
            constructor.superclass = opts.prototype.constructor.prototype;
        }
        for (var i in opts) {
            if (i != 'prototype') {
                constructor.prototype[i] = opts[i];
            }
        }
        return constructor;
    };

})();