Ruby's Louvre

每天学习一点点算法

导航

mass Framework class模块 v5

新版本进行大规模的重构,继承链与方法链被重新实现。在方法中调用父类的同名实例方法,由$super改为supermethod,保留父类的原型属性parent改为superclass。

//"use strict";
(function(global,DOC){
    var dom = global[DOC.URL.split("#")[0]];
    dom.define("class", "lang",function(){
        //=========================================
        // 核心模块 第一类工厂
        //==========================================
        var 
        P = "prototype",  C = "constructor", F = "function",I = "@init",S = "superclass",
        unextend = dom.oneObject([P,S, 'extend', 'include','toString',"supermethod"]),
        exclusive = dom.oneObject([C,I]),empty = function(){},
        classOne = dom.oneObject('Object,Array,Function');
        function expand(klass,props){
            'extend,include'.replace(dom.rword, function(name){
                var modules = props[name];
                if(classOne[dom.type(modules)]){
                    klass[name].apply(klass,[].concat(modules));
                    delete props[name];
                }
            });
            return klass
        }
        dom["@class"] =  {
            inherit : function(parent,init) {
                if(typeof parent == F){
                    for(var i in parent){//继承类方法与属性
                        this[i] = parent[i]
                    }
                    dom.mix(this[P],parent[P]);//继承原型方法与属性
                    this[S]  = parent;
                }
                //添加obj.extend,obj.include里面的对象或类到类中去
                this.toString = dom.K( (init || empty) + "");
                this[P].supermethod = function(){
                    var callee = arguments.callee;
                    var caller = callee.caller;                
                    if( caller && caller._method ){
                        return caller._method.apply(this,arguments)
                    }
                }
                var KP = this[P];
                KP[I] = (KP[I] || []).slice();
                if(typeof init == F){
                    KP[I].push(init);
                }
                KP.setOptions = function(){
                    this.options = dom.Object2.merge.apply(this.options || {}, arguments);
                    return this;
                }
                return  KP[C] = this;
            },
            extend: function(){//扩展类成员
                var middleware = {}
                for(var i = 0, module; module = arguments[i++]; ){
                    dom.mix(middleware, module);
                }
                Object.keys(middleware).forEach(function(name){
                    if(!unextend[name]){
                        this[name] =  middleware[name];;
                    }
                }, this);
                return this;
            },
            include:function(){//扩展原型成员     
                var son = this[P],parent = (this[S] || Function)[P], middleware = {}, _method, method;
                for(var i = 0, module; module = arguments[i++]; ){
                    if(dom.type(module,"Object")){
                        dom.mix(middleware, module);
                    }else if(typeof module === F){
                        dom.mix(middleware, new module);
                    }
                }
                Object.keys(middleware).forEach(function(name){
                    if(!exclusive[name]){
                        _method = parent[name];
                        method = middleware[name];
                        son[name] = method;
                        if( typeof method === F && typeof _method === F ){   
                            son[name]._method = _method;
                        }
                    }
                });
                return this;
            }    
        };
        dom.factory = function(obj){
            obj = obj || {};
            var parent  = obj.inherit //父类
            var init = obj.init ; //构造器
            delete obj.inherit;
            delete obj.init;
            var klass = function() {
                for(var i = 0 , init ; init =  this[I][i++];){
                    init.apply(this, arguments);
                }
            };
            dom.mix(klass,dom["@class"]).inherit(parent, init);//添加更多类方法
            return expand(klass,obj).include(obj);
        }
    });

})(this,this.document);
//2011.7.11
//dom["class"]改为dom["@class"]
//2011.7.25
//继承链与方法链被重新实现。
//在方法中调用父类的同名实例方法,由$super改为supermethod,保留父类的原型属性parent改为superclass

一些测试:


      dom.require("class",function(){
        var Ancestor = dom.factory({
          init:function(name){
            this.name = name;
          },
          ancestor_prop:"3333333",
          instance_fn:function(){
            return "ancestor_instance"
          },
          instance_fn2:function(){
            return "ancestor_instance2"
          },
          extend:{
            class_fn:function(){
              return "ancestor_class";
            }
          }
        });
        var Parent = dom.factory({
          inherit:Ancestor,
          instance_fn:function(){
            return this.supermethod()+"-->Parent";
          },
          extend:{
            class_fn:function(){
              return "parent_class";
            }
          }
        });
        var Son = dom.factory({
          inherit:Parent,
          init:function(name,age){
            this.age = age;
          },
          instance_fn2:function(){
            return this.supermethod()+"-->Son";
          },
          instance_fn3:function(){
            return "instance_fn3"
          },
          extend:{
            class_fn:function(){
              return "son_class";
            }
          }
        });
        var p = new Parent("john");
        dom.log(p.instance_fn());
        dom.log(Parent.class_fn());
        var s = new Son("nasami",44);
        dom.log(s.instance_fn());
        dom.log(s.age);
        dom.log(s.name);
        dom.log(s.instance_fn2());
        dom.log(Son.class_fn());
      });

相关链接:

dom Framework oop模块 v4
dom Framework oop模块 v3

posted on 2011-07-25 13:03  司徒正美  阅读(1089)  评论(1编辑  收藏  举报