web前端-js设计模式

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

1、设计模式分类:
  1. 构造函数模式(constructor)
    含义:创建一个构造函数
function Person(name,age){
  this.name=name;
  this.age=age;
}
Person.prototype.sayName=function(){
   return this.name;
}
var student=new Person("Ethan",28);
  1. 工厂模式(factory)
    含义:通常是一个函数return出来一个新的对象
function createPerson(name){
  var person={
    name: name,
    sayName:function(){
       return this.name;
     }
  }
  return person;
}
createPerson("Ethan");
this回顾:
1、方法调用模式:createPerson("Ethan").sayName(); this指向person
2、函数调用模式: var t=createPerson("Ethan").sayName;   this指向window
3、new构造对象:this指向新构造的对象
4、自动指向:apply,call,bind方法
  1. 单例模式(singleton)
    含义:每次调用这个模式不会去创造,有且只有一个(例子:组件生成的对话框,一个页面只出现一次)
var People=(function(){
  var instance;
  function init(name){
    return {
       name: name
    }
  }
  return {
    createPeople:function(name){
       if(!instance){
         instance=init(name);
       }
       return instance;
    }
  }
})();
//函数作用域:词法作用域,js作用域由function所体现,function访问的上下文由它所在定义的位置所决定
People.createPeople("Ethan");
People.createPeople("age");
  1. 混合模式(mixin)
    含义:对某个对象,增加新的东西(一般都用来混合它的原型)
function People(name,age){
  this.name=name;
  this.age=age;
}
People.prototype.sayName=function(){
  console.log(this.name);
}
var Student=function(name,age,score){
  People.call(this,name,age);
  this.score=score;
}
//用原生js方法将student.prototype指向People
Student.prototype=create(People.prototype);

function create(parentObj){
  function F(){}
  F.prototype=parentObj;
  return new F;
}
Student.prototype.sayScore=function(){
  console.log(this.score);
}
var student=new Student("Ethan","28","97")
  1. 模块模式(module)
    含义:一般通过闭包的方式实现的,特点:避免全局变量的冲突
var Person=(function(){
  var name="Ethan";
  function sayName(){
    console.log(name);
  }
  return: {
    name: name,
    sayName:sayName
  }
})();
  1. 订阅发布模式(subcribe/publish)
    含义:先订阅后发布,相当于事件监控模式,先定义好函数,再在任何时候异步同步触发事件
var EventCenter=(function(){
  var events={};    //存储所有的key/value
  //events={"hello":[{function(){}:function(){}},{function(){}:function(){}},...]}
  function on(evt,handle){
    events[evt]=events[evt] || [];
    events[evt].push({
      handle:handle
    });
    //events["hello"]=[{handle:handle}]
  }
  function fire(evt,args){
    if(!events[evt]) return;
    for(var i=0;i<events[evt].length;i++){
      events[evt][i].handle(args);
    }
  }
function off(name){
    delete events[name];
  }
  return {
    on:on,
    fire:fire,
    off:off
  };
})();
                 
EventCenter.on("hello",function(word){
  console.log(word);
});
EventCenter.fire("hello","word");

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

Event.on('change', function(val){
    console.log('change...  now val is ' + val);  
});
Event.fire('change', 'Ethan');
Event.off('changer');
var Event=(function(){
  var events={};    //存储所有的key/value
  //events={"hello":[{function(){}:function(){}},{function(){}:function(){}},...]}
  function on(evt,handle){
    events[evt]=events[evt] || [];
    events[evt].push({
      handle:handle
    });
    //events["hello"]=[{handle:handle}]
  }
  function fire(evt,args){
    if(!events[evt]) return;
    for(var i=0;i<events[evt].length;i++){
      events[evt][i].handle(args);
    }
  }
function off(name){
    delete events[name];
  }
  return {
    on:on,
    fire:fire,
    off:off
  };
})();

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

(mission 6)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 工厂模式类似于现实生活中的工厂可以产生大量相似的商品,去做同样的事情,实现同样的效果;这时候需要使用工厂模式。简单...
    舟渔行舟阅读 7,827评论 2 17
  • 1.几种基本数据类型?复杂数据类型?值类型和引用数据类型?堆栈数据结构? 基本数据类型:Undefined、Nul...
    极乐君阅读 5,574评论 0 106
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,053评论 25 708
  • 动作: 中指向徽弹出曰“剔”。中指微曲中、末二节,甲背着弦,下指不可太深,太深就会滞碍。须当空落指,其运动在中、末...
    圣易王时阅读 750评论 0 0
  • 海棠社第114社 主题:以“酒力渐浓春思荡”入作 要求:酒力渐浓春思荡可以理解为春天的午后喝了酒,荡起了伤春的思绪...
    刘寒霜阅读 599评论 33 19