美文网首页
前端设计模式

前端设计模式

作者: S级食材咩咩羊 | 来源:发表于2017-05-08 22:50 被阅读0次

1. 写出构造函数模式、混合模式、模块模式、工厂模式、单例模式、发布订阅模式的范例

<script>
  <!-- 模块模式 -->
  <!-- 由于函数作用域无法获取内部变量,故进行包装 -->
  var Person = (function(){
    var name = 'poi';
    function sayName() {
      console.log(name);
    }
    return {
      name: name,
      sayName: sayName
    }
  })()
  <!---构造函数模式 -->
  function Person(name, age) {
    this.name = name;
    this.age = age;
  }
  Person.prototype.sayName = function(){
    return this.name;
  };
  var student = new Person("poi", 30);
  console.log(student);
  <!-- 混合模式(继承) -->
  var Person = function(name, age) {
    this.name = name;
    this.age = age;
  };
  Person.prototype.sayName = function(){
    console.log(this.name);
  };
  var Student = function(name, age, score) {
    Person.call(this, name, age)
    this.score = score;
  };
  Student.prototype = create(Person.prototype);
  function create(parentObj){
    function F(){};
    F.prototype = parentObj;
    return new F();
  };

  Student.prototype.sayscore = function(){
    console.log(this.score);
  }

  var student = new Student("poi", 28, 29);
  console.log(student);
  <!-- 工厂模式 -->
  function createPerson(opts){
    var person = {
      name: opts.name || 'poi'
    };
    person.sayName = function() {
      console.log(this.name);
    }
    return person;
  }
  var p1 = createPerson({name:'poi'});
  var p2 = createPerson({name: 'yamato'});
  <!-- 单例模式- -->
  var Car = (function(){
    var instance;
    function init() {
      var speed = 0;
      return {
        getSpeed: function(){
          return speed;
        },
        setSpeed: function(s){
          speed = s;
        }
      };
    }
    return {
      create: function() {
        if(!instance) {
          instance = init();
        }
        return instance;
      }
    };
  })();

  var car1 = Car.create();
  var car2 = Car.create();
  car1 === car2;
  <!--发布订阅模式 -->
  var Event = (function() {
    var events = {};

    function on(evt, handler) {
      events[evt] = events[evt] || [];
      events[evt].push({
        handler: handler
      });
    }

    function fire(evt, args) {
      if(!events[evt]){
        return;
      }
      for(var i=0; i<events[evt].length; i++){
        events[evt][i].handler(args);
      }
    }

    function off(evt) {
      delete events[evt];
    }

    return {
      on: on,
      fire: fire,
      off: off
    }
  }());

  Event.on('change', function(val){
      console.log('change...  now val is ' + val);
  });
  Event.fire('change', '饥人谷'); //change...  now val is 饥人谷
  Event.off('change');

</script>

2. 使用发布订阅模式写一个事件管理器,可以实现如下方式调用

  <!--发布订阅模式 -->
  var Event = (function() {
    var events = {};

    function on(evt, handler) {
      events[evt] = events[evt] || [];
      events[evt].push({
        handler: handler
      });
    }

    function fire(evt, args) {
      if(!events[evt]){
        return;
      }
      for(var i=0; i<events[evt].length; i++){
        events[evt][i].handler(args);
      }
    }

    function off(evt) {
      delete events[evt];
    }

    return {
      on: on,
      fire: fire,
      off: off
    }
  }());

  Event.on('change', function(val){
      console.log('change...  now val is ' + val);
  });
  Event.fire('change', '饥人谷'); //change...  now val is 饥人谷
  Event.off('change');

</script>

相关文章

网友评论

      本文标题:前端设计模式

      本文链接:https://www.haomeiwen.com/subject/vjwvtxtx.html