mass Framework classモジュールv 12

5529 ワード

最近、クラスファクトリにアップグレードされ、メソッドチェーン、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);
            })