beauty("$Class",["$underscore"],function(_){


   var Class = function () {
        var length = arguments.length;
        var option = arguments[length - 1];

        option.init = option.init || function () {
        };

        // 如果参数中有要继承的父类
        if (length === 2) {
            /**
             * @ignore
             */
            var superClass = arguments[0].extend;

            /**
             * @ignore
             */
            var tempClass = function () {
            };
            tempClass.prototype = superClass.prototype;

            /**
             * @ignore
             */
            var subClass = function () {
                this.init.apply(this, arguments);
            };

            // 加一个对父类原型引用的静态属性
            subClass.superClass = superClass.prototype;
            //subClass.superClass = superClass;
            /**
             * @ignore
             */
            subClass.callSuper = function (context, func) {
                var slice = Array.prototype.slice;
                var a = slice.call(arguments, 2);
                var func = subClass.superClass[func];
                //var func = subClass.superClass.prototype[func];
                if (func) {
                    func.apply(context, a.concat(slice.call(arguments)));
                }
            };

            // 指定原型
            subClass.prototype = new tempClass();

            // 重新指定构造函数
            subClass.prototype.constructor = subClass;

            _.extend(subClass.prototype, option);

            /**
             * @ignore
             */
            subClass.prototype.init = function () {
                // 调用父类的构造函数
                // subClass.superClass.init.apply(this, arguments);
                // 调用此类自身的构造函数
                option.init.apply(this, arguments);
            };

            return subClass;

            // 如果参数中没有父类,则单纯构建一个类
        } else {
            if (length === 1) {
                /**
                 * @ignore
                 */
                var newClass = function () {
                    // 加了return,否则init返回的对象不生效
                    return this.init.apply(this, arguments);
                };
                newClass.prototype = option;
                return newClass;
            }
        }


    };


    beauty.$superPackage("$Class",Class);


});

  

相关文章:

  • 2021-11-17
  • 2022-01-16
  • 2021-06-24
  • 2022-12-23
  • 2021-07-06
  • 2021-09-06
  • 2022-12-23
猜你喜欢
  • 2021-10-05
  • 2022-12-23
  • 2022-01-30
  • 2022-12-23
  • 2022-12-24
  • 2022-01-24
  • 2022-02-02
相关资源
相似解决方案