zoukankan      html  css  js  c++  java
  • 【JavaScript】JavaScript模拟Class

    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);
    
    
    });
    

      

  • 相关阅读:
    java基础篇6之代理
    JavaWeb 过滤器应用之页面静态化
    JavaWeb 之过滤器
    JavaWeb 之监听器
    分页
    Linux 入门
    多条件组合查询
    Service 事务(JdbcUtils 升级)
    BaseServlet 介绍
    dbUtils 工具类介绍
  • 原文地址:https://www.cnblogs.com/lhp2012/p/4809421.html
Copyright © 2011-2022 走看看