Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Namespacing in JavaScript

RSS
Modified on Thu, Aug 25, 2011, 6:34 PM by Administrator Categorized as JavaScript, jQuery, and Angular

jsContract.js

/*jslint evil: true, browser: true, immed: true, passfail: true, undef: true, newcap: true*/

var Contract = (function () {
    ///<summary> class Contract </summary>   
    /// <returns type="Contract" />

    function ContractError(message, fileName, lineNumber) {
        ///<summary> class ContractError </summary>
        /// <param name="message" type="String"> args	</param>
        /// <param name="fileName" type="String"> args	</param>
        /// <param name="lineNumber" type="Number"> args	</param>
        /// <returns type="ContractError" />

        this.message = message;
        this.fileName = fileName;
        this.lineNumber = lineNumber;
    }
    ContractError.prototype = new Error();
    ContractError.prototype.constructor = ContractError;
    ContractError.prototype.name = 'ContractError';


    function ofType(obj, type) {
        return (typeof obj === type);
    }

    function getMessage(base, msg) {
        return base + ((typeof msg === "string") ? (" - '" + msg + "'.") : ".");
    }

    function instanceOf(arg, type, msg) {
        if (!(arg instanceof type)) {
            msg = getMessage("The argument was not an instance of type: " + type.name, msg);
            throw new ContractError(msg);
        }
    }

    // These methods are used for preconditions
    function expectType(arg, type, msg) {
        var argType = typeof arg;
        if (argType !== type) {
            msg = getMessage("The argument was not of expected type: '" + type + "' - was '" + argType + "'", msg);
            throw new ContractError(msg);
        }
    }

    function expect(condition, msg) {
        ///<summary> Enforce condition </summary>
        /// <param name="condition" type="Boolean"> any statement or value that evaluates like an if(condition) </param>
        /// <param name="msg" type="String"> Message if condition is not met </param>    
        /// <returns type="void" />
        if (!condition) {
            throw new ContractError(getMessage("The required condition was not met", msg));
        }
    }
    function expectObject(arg, msg) {
        expectType(arg, "object", msg);
    }
    function expectNumber(arg, msg) {
        expectType(arg, "number", msg);
    }
    function expectString(arg, msg) {
        expectType(arg, "string", msg);
    }
    function expectRegExp(arg, msg) {
        instanceOf(arg, RegExp, msg);
    }
    function expectDOMElement(arg, msg) {
        throw new Error("Not implemented");
    }

    function expectValue(arg, values, msg) {
        if (!values instanceof Array) {
            values = [values];
        }
        var i = values.length;
        while (i--) {
            if (arg === values[i]) {
                return;
            }
        }
        throw new ContractError(getMessage("argument has an invalid value, '" + arg + "' not in '" + values.join() + "'", msg));
    }

    function expectWhen(precondition, condition, msg) {
        if (precondition) {
            expect(condition, msg);
        }
    }

    return {
        expect: expect,
        expectWhen: expectWhen,
        expectObject: expectObject,
        expectNumber: expectNumber,
        expectString: expectString,
        expectRegExp: expectRegExp,
        expectValue: expectValue
    };

} ());

RootNamespace.js

var SmartPdm = {
    alert: function (alertText) {
        /// <summary> Creates a  Hello World Alert.  This is only to test intellisense</summary>
        /// <returns type="void" />
        alert(alertText);
    },
    otherFunction: function() {
        // do something else
    }
};

RootNamespace.Subspace.js

/// <reference path="jsContract.js" />
/// <reference path="RootNamespace.js" />
/*===============================================================================================*/
$(document).ready(function () {
    RootNamespace.Subspace.init();
});
/*===============================================================================================*/
(function ($) {

    Contract.expect(RootNamespace, "RootNamespacenamespace not loaded");

    $.extend(RootNamespace, {
        Subspace: {
            /*-----------------------------------------------------------------------------------*/
            init: function() {
                // do document.ready stuff here
            },
            /*-----------------------------------------------------------------------------------*/
            test: function() { alert('This is a test.'); }
        } /* RootNamespace.Subspace namespace */
    }); /* extend */
})(jQuery); /* function ($) */
/*===============================================================================================*/

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.