zoukankan      html  css  js  c++  java
  • 使用闭包来实现一个完整的面向对象系统

    //面向对象的方式
    var Tv={
      open:function(){
        alert('打开');
      },
      close:function(){
        alert('关闭');
      }
    };
    
    var CreateCommand = function(rececier){
      this.rececier = rececier;
    };
    CreateCommand.prototype = {
      execute : function(){
        this.rececier.open();
      },
      undo : function(){
        this.rececier.close();
      }
    };
    var setCommand = function(command){
      document.getElementById('execute').onclick = function(){
      command.execute();
      };
      document.getElementById('undo').onclick = function(){
      command.undo();
      };
    };
    
    setCommand( new CreateCommand(Tv));
    
    
    //闭包的方式
    var Tv={
      open:function(){
        alert('打开');
      },
      close:function(){
        alert('关闭');
      }
    };
    
    var CreateCommand = function (rececier){
      var execute = function(){
        rececier.open();
      };
      var undo = function(){
      rececier.close();
      };
    
      return {
        execute:execute,
        undo:undo
       };
    };
    
    var setCommand = function(command){
        document.getElementById('execute').onclick = function(){
        command.execute();
      };
        document.getElementById('undo').onclick = function(){
        command.undo();
      };
    };
    
    setCommand( CreateCommand(Tv));

     摘自JavaScript设计模式与开发实践

  • 相关阅读:
    Python【每日一问】38
    Python【每日一问】37
    Shell~echo -e 颜色输出
    Python【每日一问】36
    Python【每日一问】35
    聊聊、Java 命令 第二篇
    聊聊、RabbitMQ 配置文件
    聊聊、Java 命令 第一篇
    聊聊、CA机构认证CSR生成
    聊聊、Tomcat中文乱码和JVM设置
  • 原文地址:https://www.cnblogs.com/cszdsb/p/6419810.html
Copyright © 2011-2022 走看看