m0_54541457 2022-02-21 10:56 采纳率: 66.7%
浏览 23
已结题

“类”设计模式的案例解析

背景:这是一段“类”设计模式案例:设计2个控制器,一个用来操作网页中的登陆表单,一个用来与服务器进行验证。案例中设计了一个Controller父类,并关联了2个子类LoginController(操作登陆表单),AuthController(与服务器进行验证)。

问题:具体有几段代码不理解其中的含义,已在代码中标注指出,请高人帮忙详细解析下。感谢!

       //父类
      function Controller() {
        this.errors = [];
      }
      Controller.prototype.showDialog = function (title, msg) {
        // 给用户显示标题和消息
      };
      Controller.prototype.success = function (msg) {
        this.showDiglog("Success", msg);
      };
      Controller.prototype.failure = function (err) {
        this.errors.push(err);
        this.showDialog("Error", err);
      };

      //子类 LoginController
      function LoginController() {
        Controller.call(this);
      }
      //把子类关联到父类
      LoginController.prototype = Object.create(Controller.prototype);

      LoginController.prototype.getUser = function () {
        return document.getElementById("login_username").value;
      };

      LoginController.prototype.getPassward = function () {
        return document.getElementById("login_password").value;
      };

      LoginController.prototype.validateEntry = function (user, pw) {
        var user = user || this.getUser();
        var pw = pw || this.getPassward();

        if (!(user && pw)) {
          return this.failure("Please enter a username & password!");
        } else if (pw.length < 5) {
          return this.failure("Password must be 5+ characters!");
        }
        return true;
      };

      LoginController.prototype.failure = function (err) {
        Controller.prototype.failure.call(this, "Login invalid: " + err);
      };

      // 子类AuthController
      function AuthController(login) {
        Controller.call(this);
        this.login = login;
      }

      //把子类关联到父类
      AuthController.prototype = Object.create(Controller.prototype);
      console.log(AuthController);

      AuthController.prototype.server = function (url, data) {
        return $.ajax({
          url: url,
          data: data,
        });
      };

      AuthController.prototype.checkAuth = function () {
        //如何理解this.login.???  this.login 不是function
        var user = this.login.getUser(); 
        //问题同上
        var pw = this.login.getPassward();
        //问题同上
        if (this.login.validateEntry(user, pw)) {
          this.server("/check-auth", { user: user, pw: pw })
            .then(this.success.bind(this))
            .fail(this.failure.bind(this));
        }
      };

      AuthController.prototype.success = function () {
        Controller.prototype.success.call(this, "Authienticated!");
      };

      AuthController.prototype.failure = function () {
        Controller.prototype.failure.call(this, "Auth Failed:" + err);
      };

      var auth = new AuthController();
      auth.checkAuth(
        // 这段代码含义?
        new LoginController() 
      );


  • 写回答

2条回答 默认 最新

  • 於黾 2022-02-21 11:16
    关注

    this.login = login;
    这不是从这取的值吗,50行
    你这关键代码不全,怎么继承的都没体现,怎么实例化的也没有,都没有AuthController(login)的调用,
    所以只能知道this.login就是获取了当时传进来的登陆状态,是把传入的参数保存到全局变量里了
    但是login具体是个什么结构看不出来,按理说应该是LoginController的实例才对

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月2日
  • 已采纳回答 2月22日
  • 创建了问题 2月21日

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助