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设计模式与开发实践

  • 相关阅读:
    php实现rpc简单的方法
    统计代码量
    laravel的速查表
    header的参数不能带下划线
    PHP简单实现单点登录功能示例
    phpStorm函数注释的设置
    数据结构基础
    laravel生命周期和核心思想
    深入理解php底层:php生命周期
    Jmeter:实例(性能测试目标)
  • 原文地址:https://www.cnblogs.com/cszdsb/p/6419810.html
Copyright © 2011-2022 走看看