Ruby's Louvre

每天学习一点点算法

导航

mass Framework oop模块 v3

着手开发更新的版本,把旧的版本放出来作记念。


"use strict";
(function(global,DOC){
    var dom = global[DOC.URL.split("#")[0]];
    dom.define("oop", ["lang"],function(){
        //=========================================
        // 核心模块 第一类工厂
        //==========================================
        var PROTO = "prototype",  CTOR = "constructor";
        var arr_extend = [PROTO,  'extend', 'include','inherit','ancestors','parent'];
        var arr_include = [CTOR];
        var one_object = dom.oneObject(['Object',"Array"]);
        function add_modules(klass,props){
            'extend,include'.replace(/\w+/g, function(name){
                var modules = props[name];
                if(one_object[dom.type(modules)]){
                    klass[name].apply(klass,[].concat(modules));
                    delete props[name];
                }
            });
        }
        function clean_module(module,props){
            for(var name in props){
                delete module[name]
            }
            return module;
        }
        var class_methods =  {
            inherit : function(parent) {
                if (parent && parent[PROTO]) {
                    this[PROTO] = Object.create(parent[PROTO]);//高效设置原型链
                    this.parent = parent;
                }
                this.ancestors =  [];
                while (parent) {//收集所有父类,用于构建方法链时查找同名方法
                    this.ancestors.push(parent);
                    parent = parent.parent;
                }
                return this[PROTO][CTOR] = this;
            },
            extend: function(){//扩展类成员
                for(var i = 0, n = arguments.length; i < n ; i++){
                    if(dom.type(arguments[i],"Object")){
                        dom.mix(this, clean_module(arguments[i],arr_extend))
                    }
                }
                return this;
            },
            include:function(){//扩展原型成员
                var parents = [this].concat(this.ancestors), target = this[PROTO],modules = [], module;
                for(var i = 0, n = arguments.length; i < n ; i++){
                    module = arguments[i];
                    if(dom.type(module,"Object")){
                        modules.push(module);
                    }else if(typeof module === "function"){
                        modules.push(new module);
                    }
                }
                dom.lang(modules).forEach(function(module){
                    dom.lang( clean_module(module,arr_include)).forEach(function(method,name){
                        var i = 0,parent,super_method;
                        while((parent = parents[i++])){
                            if (parent[PROTO] && name in parent[PROTO]) {
                                super_method = parent[PROTO][name];
                                break;
                            }
                        }
                        if( typeof method === "function" && typeof super_method === "function" ){
                            target[name] =  function() {
                                this.$super = super_method;
                                return method.apply(this, arguments);
                            }
                            target[name].toString = dom.K(method + "");
                        }else{
                            target[name] = method;
                        }
                    });
                });
                return this;
            }
        };
        dom.oop = function(obj){
            obj = obj || {};
            var superclass = obj.inherit || {}; //父类
            delete obj.inherit;
            obj.nonew = !!obj.nonew;//不用new关键字进行实例化
            var klass = function() {
                var that = this;
                if(!(that instanceof klass)){
                    if(typeof obj.unnew === "function"){
                        return obj.unnew.apply(klass,arguments);
                    }
                    if(obj.nonew){
                        that = new klass;
                        return that.init && that.init.apply(that,arguments);
                    }
                }
                return that.init && that.init.apply(that,arguments);
            };
            dom.mix(klass,class_methods).inherit(superclass).extend(superclass);
            add_modules(klass,obj);
            klass.toString = dom.K(obj.init+"");
            return klass.include(obj);
        }
        dom.alias();
    });

})(this,this.document);

posted on 2011-04-19 09:42  司徒正美  阅读(1230)  评论(0编辑  收藏  举报