mass Framework oop 모듈 v3

5234 단어 framework
업데이트된 버전을 개발하는 데 착수하여 낡은 버전을 내놓아 기념하다.


"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);

좋은 웹페이지 즐겨찾기