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

  • 相关阅读:
    【Java&Android开源库代码分析】のandroid-async-http の开盘
    静态库和动态库
    抽象工厂
    XXX系统发展综述(SSH+Jquery EasyUI)
    android 控制手机的体积的大小 切换音效模式
    中国误区,你还抓?
    PID教程
    setsockopt的作用
    【ThinkingInC++】66、pointer Stash的使用
    jbpm入门样例
  • 原文地址:https://www.cnblogs.com/cszdsb/p/6419810.html
Copyright © 2011-2022 走看看