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,854評論 2 17
  • 1.幾種基本數據類型?復雜數據類型?值類型和引用數據類型?堆棧數據結構? 基本數據類型:Undefined、Nul...
    極樂君閱讀 5,603評論 0 106
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,569評論 25 708
  • 動作: 中指向徽彈出曰“剔”。中指微曲中、末二節,甲背著弦,下指不可太深,太深就會滯礙。須當空落指,其運動在中、末...
    圣易王時閱讀 751評論 0 0
  • 海棠社第114社 主題:以“酒力漸濃春思蕩”入作 要求:酒力漸濃春思蕩可以理解為春天的午后喝了酒,蕩起了傷春的思緒...
    劉寒霜閱讀 603評論 33 19