js Class

(function(window, undefined) {

    ///Classes (or prototypial inheritors)
    if (typeof Object.create !== "function") {
        Object.create = function(o) {
            function F() { }
            F.prototype = o;
            return new F();
        };
    }

    ///Object properties
    var Class = {
        Init: function() {
            ///<summary> Constructor
            ///</summary>
        },
        base: function(obj) {
            ///<summary>Inherit
            ///<para>object.parent parent is properties to strengthen</para>
            ///</summary>
            var object = Object.create(obj);
            ///constructor and prototype inheritance patterns
            if (typeof object.Init !== "function") {
                object.Init = function() { };
            }
            ///prototype copy
            object.Init.prototype = new obj.Init();
            object.parent = obj;
            return object;
        },
        extend: function(obj) {
            ///<summary>extend class method and property (static)
            ///</summary>
            for (var i in obj) {
                this[i] = obj[i];
            }
            return this;
        },
        include: function(obj) {
            ///<summary> include instance of method and property (public)
            ///</summary>   
            var fn = this.Init.prototype;
            for (var i in obj) {
                fn[i] = obj[i];
            }
            return this;
        }
    }

    /// Single responsibility to create a class
    var Gas = {
        ///Function entry type is function
        init: {},
        create: function() {
            ///<summary> create class
            ///</summary>
            var object = Object.create(Class);
            return object;
        }
    }

    window.Gas = Gas;
})(window);

posted @ 2012-07-18 16:33  happyzhu  阅读(295)  评论(0编辑  收藏  举报