zoukankan      html  css  js  c++  java
  • 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);
    
  • 相关阅读:
    理解 QEMU/KVM 和 Ceph(2):QEMU 的 RBD 块驱动(block driver)
    Neutron VxLAN + Linux Bridge 环境中的网络 MTU
    理解 QEMU/KVM 和 Ceph(1):QEMU-KVM 和 Ceph RBD 的 缓存机制总结
    [译] 企业级 OpenStack 的六大需求(第 3 部分):弹性架构、全球交付
    [译] 企业级 OpenStack 的六大需求(第 2 部分):开放架构和混合云兼容
    [译] 企业级 OpenStack 的六大需求(第 1 部分):API 高可用、管理和安全
    Javascript中的Array(数组) 、{}(映射) 与JSON解析
    HTML中显示特殊字符,如尖括号 “<”,">"等等
    Ubuntu 12.04 安装配置 Apache2
    Python中函数的参数传递与可变长参数
  • 原文地址:https://www.cnblogs.com/rubylouvre/p/2020519.html
Copyright © 2011-2022 走看看