Search

Writing a framework in JavaScript

How to create a framework with Javascript

Although JavaScript is a loosely type language with unstructured code snippets but still it can be used in a object oriented way to create an framework like this. The following code is self explationary with comments so that even a basic developer with OOP concept can follow it.

// App framework using JavaScript
(function () {
    var JSFramework = (function () {

        // private var definition
        var canvas = null,
            ctx = null,
            resultFn = null,

        // private functions
        initObject = function () {
            // initialize private varibles
        },

        privateFunctionOne = function (id) {
            //private function with parameter
        },

        privateFunctionWithCallback = function () {
            // private function with callback

            //callback
            callbackHandler(event.data);
        },

        //callback handler
        callbackHandler = function (result) {
            // process the result
            console.log("callback handler gets: " + result);
        }

        // public interface
        return {
            init:function () {
                initObject();
            },
            load:function (param) {
                if (typeof (param) == "string") {
                    privateFunctionOne(param);
                }
            },
            scan:function (fn) {
                if (arguments.length > 0 && typeof (arguments[0]) == "function") {
                    resultFn = fn;
                }
                privateFunctionWithCallback();
            }
        };
    })();

    // register framework at window object
    if (!window.fwrkName)
        window.fwrkName = JSFramework;

    // and initialize it
    JSFramework.init();
})();