• mass Framework class模块v12


    最近为类工厂升级,为它添加了许多时麾的功能,如方法链,extend子类生产器。

    //=========================================
    // 类工厂模块 v12 by 司徒正美
    //==========================================
    define("class", ["lang"], function($) {
        function bridge() {
        }
        var fnTest = /mass/.test(function() {
            mass;
        }) ? /\b_super|_superApply\b/ : /.*/;
    
        var hash = {
            inherit: function(parent, init) {
                //继承一个父类,并将它放进_init列表中,并添加setOptions原型方法
                if (typeof parent == "function") {
                    for (var i in parent) { //继承类成员
                        this[i] = parent[i];
                    }
                    bridge.prototype = parent.prototype;
                    this.prototype = new bridge; //继承原型成员
                    this._super = parent; //指定父类
                    if (!this.__init__) {
                        this.__init__ = [parent]
                    }
                }
                this.__init__ = (this.__init__ || []).concat();
                if (init) {
                    this.__init__.push(init);
                }
                this.toString = function() {
                    return(init || bridge) + "";
                }
                var proto = this.fn = this.prototype;
                proto.extend = hash.extend;
                proto.setOptions = function() {
                    var first = arguments[0];
                    if (typeof first === "string") {
                        first = this[first] || (this[first] = {});
                        [].splice.call(arguments, 0, 1, first);
                    } else {
                        [].unshift.call(arguments, this);
                    }
                    $.Object.merge.apply(null, arguments);
                    return this;
                }
                return proto.constructor = this;
            },
            extend: function(module) {
                //添加一组原型方法
                var target = this;
                Object.keys(module).forEach(function(name) {
                    var fn = target[name], fn2 = module[name]
                    if (typeof fn === "funciton" && typeof fn2 === "function" && fnTest.test(fn2)) {
                        var __super = function() { //创建方法链
                            return fn.apply(this, arguments);
                        };
                        var __superApply = function(args) {
                            return fn.apply(this, args);
                        };
                        target[name] = function() {
                            var t1 = this._super;
                            var t2 = this._superApply;
                            this._super = __super;
                            this._superApply = __superApply;
                            var ret = fn2.apply(this, arguments);
                            this._super = t1;
                            this._superApply = t2;
                            return ret;
                        };
                    } else {
                        target[name] = fn2;
                    }
                });
                return this;
            }
        };
        function getSubClass(obj) {
            return  $.factory(this, obj);
        }
        $.factory = function(parent, obj) {
            if (arguments.length === 1) {
                obj = parent;
                parent = null;
            }
            var statics = obj.statics;//静态成员扩展包
            var init = obj.init; //构造器
            delete obj.init;
            delete obj.statics;
            var klass = function() {
                for (var i = 0, init; init = klass.__init__[i++]; ) {
                    init.apply(this, arguments);
                }
            };
            hash.inherit.call(klass, parent, init);//继承了父类原型成员与类成员
            var fn = klass.fn;
            var __init__ = klass.__init__;
            $.mix(klass, statics);//添加类成员
            klass.prototype = klass.fn = fn;
            klass.__init__ = __init__;
            klass.fn.extend(obj);
            klass.mix = $.mix;
            klass.extend = getSubClass;
            return klass;
        };
        $.mix($.factory, hash);
        return $
    });
    

    例子:

                require("class", function($) {
                    var Animal = $.factory({
                        init: function(name) {
                            this.name = name
                        },
                        getName: function() {
                            return this.name;
                        },
                        setName: function(name) {
                            this.name = name;
                        }
                    });
                    var a = new Animal("zzz");
                    var Dog = $.factory(Animal, {
                        init: function(name, age) {
                            this.age = age;
                        },
                        getAge: function() {
                            return this.age;
                        },
                        setAge: function(age) {
                            this.age = age;
                        }
                    });
                    var dog = new Dog("dog", 222);
                    console.log(dog);
                })
    
    机器瞎学/数据掩埋/模式混淆/人工智障/深度遗忘/神经掉线/计算机幻觉/专注单身二十五年
  • 相关阅读:
    不要控制!
    【转】iframe页面跳转时,导致父页面滚动!该怎么解决?
    【转】XML 特殊字符处理
    【转】使用Log4Net进行日志记录
    【转】JS获取浏览器可视区域的尺寸
    【转】Winform程序未捕获异常解决方法 EventType clr20r3 P1
    【转】VMware Tools installation cannot be started manually while Easy Install is in progress.
    如何解决安装VMware后郑广电宽带客户端不能登录的问题?
    MVC中的M是ViewModel不是EntityModel!
    纸上原型--纸上草稿设计--简单高效的沟通方式!
  • 原文地址:https://www.cnblogs.com/rubylouvre/p/2933671.html
Copyright © 2020-2023  润新知