简易javascript框架

简易javascript框架

(function () {
    var moduleMap = {};
    var fileMap = {};

    var noop = function () {
    };

    var thin = {
        define: function(name, dependencies, factory) {
            if (!moduleMap[name]) {
                var module = {
                    name: name,
                    dependencies: dependencies,
                    factory: factory
                };

                moduleMap[name] = module;
            }

            return moduleMap[name];
        },

        use: function(name) {
            var module = moduleMap[name];

            if (!module.entity) {
                var args = [];
                for (var i=0; i<module.dependencies.length; i++) {
                    if (moduleMap[module.dependencies[i]].entity) {
                        args.push(moduleMap[module.dependencies[i]].entity);
                    }
                    else {
                        args.push(this.use(module.dependencies[i]));
                    }
                }

                module.entity = module.factory.apply(noop, args);
            }

            return module.entity;
        },

        require: function (pathArr, callback) {
            for (var i = 0; i < pathArr.length; i++) {
                var path = pathArr[i];

                if (!fileMap[path]) {
                    var head = document.getElementsByTagName('head')[0];
                    var node = document.createElement('script');
                    node.type = 'text/javascript';
                    node.async = 'true';
                    node.src = path + '.js';
                    node.onload = function () {
                        fileMap[path] = true;
                        head.removeChild(node);
                        checkAllFiles();
                    };
                    head.appendChild(node);
                }
            }

            function checkAllFiles() {
                var allLoaded = true;
                for (var i = 0; i < pathArr.length; i++) {
                    if (!fileMap[pathArr[i]]) {
                        allLoaded = false;
                        break;
                    }
                }

                if (allLoaded) {
                    callback();
                }
            }
        }
    };

    window.thin = thin;
})();
测试代码如下:

thin.define("constant.PI", [], function() {
    return 3.14159;
});

thin.define("shape.Circle", ["constant.PI"], function(pi) {
    var Circle = function(r) {
        this.r = r;
    };

    Circle.prototype = {
        area : function() {
            return pi * this.r * this.r;
        }
    }

    return Circle;
});

thin.define("shape.Rectangle", [], function() {
    var Rectangle = function(l, w) {
        this.l = l;
        this.w = w;
    };

    Rectangle.prototype = {
        area: function() {
            return this.l * this.w;
        }
    };

    return Rectangle;
});

thin.define("ShapeTypes", ["shape.Circle", "shape.Rectangle"], function(Circle, Rectangle) {
    return {
        CIRCLE: Circle,
        RECTANGLE: Rectangle
    };
});

thin.define("ShapeFactory", ["ShapeTypes"], function(ShapeTypes) {
    return {
        getShape: function(type) {
            var shape;

            switch (type) {
                case "CIRCLE": {
                    shape = new ShapeTypes[type](arguments[1]);
                    break;
                }
                case "RECTANGLE":  {
                    shape = new ShapeTypes[type](arguments[1], arguments[2]);
                    break;
                }
            }

            return shape;
        }
    };
});

var ShapeFactory = thin.use("ShapeFactory");
alert(ShapeFactory.getShape("CIRCLE", 5).area());
alert(ShapeFactory.getShape("RECTANGLE", 3, 4).area());


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值