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

  • 相关阅读:
    MVC————前台中,冒号与等号的区别
    MVC-通过对象获取整个表单内容
    对Webservice的理解
    windows上使用logstash-input-jdbc
    elasticsearch-head的安装和使用
    最简单的php学习
    linq to sql 和linq to php 的区别
    thinkphp中JS文件不能写__ROOT__变量
    用curl获取https请求时出现错误的处理
    优化apk的odex处理
  • 原文地址:https://www.cnblogs.com/cszdsb/p/6419810.html
Copyright © 2011-2022 走看看