16.第四篇:行為型設(shè)計模式

本文摘自 《JavaScript 設(shè)計模式》張容銘 著 版權(quán)歸原作者所有

模板方法模式

模板的原型方法

var Alert = function(){};
Alert.prototype = {
  // 創(chuàng)建方法
  init:function(){
    // 生成提示框
    this.panel.appendChild(this.closeBtn);
    this.panel.appendChild(this.contentNode);
    this.panel.appendChild(this.confirmBtn);
    // 插入頁面中
    document.body.appendChild(this.panel);
    // 綁定事件
    this.bindEvent();
    // 顯示提示框
    this.show();
  },
  bindEvent : function(){
    var me = this;
    // 關(guān)閉按鈕點擊事件
    this.closeBtn.onclick = function(){
      // 執(zhí)行關(guān)閉取消方法
      me.fail();
      // 隱藏彈層
      me.hide();
    },
  },
  // 隱藏彈層方法
  hide : function(){
        this.panel.style.display = 'none';
  },
  // 顯示彈層方法
  show : function(){
    this.panel.style.display = 'block';
  }
}

根據(jù)模板創(chuàng)建類

// 右側(cè)按鈕提示框
var RAlert = function(data){
  // 繼承基本提示框構(gòu)造函數(shù)
  Alert.call(this,data);
  // 為確認按鈕添加right類設(shè)置位置居右
  this.confirmBtn.className = this.confirmBtn.className + 'right';  
}
// 繼承基本提示框方法
RAlert.prototype = new Alert();

// 標題提示框
var TitleAlert = function(data){
  // 繼承基本提示框構(gòu)造函數(shù)
  Alert.call(this,data);
  // 設(shè)置標題內(nèi)容
  this.title = data.title;
  // 創(chuàng)建標題組件
  this.titleNode = document.creareElement('h3');
  // 標題組件中寫入標題內(nèi)容
  this.titleNode.innerHTML = this.title;
}
// 繼承基本提示框方法
TitileAlert.prototype = new Alert();
// 對基本提示框創(chuàng)建方法拓展
TitleAlert.prototype.init = function(){
  // 插入標題
  this.panel.inserBefore(this.titileNode,this.panel.firstChild);
  // 繼承基本提示框init方法
  Alert.prototype.init.call(this); 
}

繼承類也可作為模板類

// 帶有取消按鈕的彈出框
var CancelAlert = function(data){
  // 繼承標題提示框構(gòu)造函數(shù)
  TitleAlert.call(this,data);
  // 取消按鈕文案
  this.canel = data.cancel;
  // 創(chuàng)建取消按鈕
  this.cancelBrn = document.createElement('span');
  // 為取消按鈕添加類
  this.cancelBtn.className = 'cancel';
  // 設(shè)置取消按鈕內(nèi)容
  this.cancelBtn.innerHTML = this.cancel || '取消';
}
// 繼承標題提示框原型方法
CancelAlert.prototype= newAlert();
CancelAlert.prototype.init = function(){
  var me = this;
  // 標題提示框綁定事件方法繼承
  TitleAlert.prototype.bindEvent.call(me);
  // 取消按鈕綁定事件
  this.cancelBtn.onclick = function(){
    // 執(zhí)行取消回調(diào)函數(shù)
    me.fail();
    // 隱藏彈層
    me.hide();
  }
}

創(chuàng)建一個提示框

new CancelAlert({
  title:'提示標題',
  content:'提示內(nèi)容',
  success:function(){
    console.log('ok');
  },
  fail:function(){
    console.log('cancel')
  }
}).init();
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • 接觸前端兩三個月的時候,那時候只是聽說設(shè)計模式很重要,然后我就去讀了一本設(shè)計模式的書,讀了一部分,也不知道這些設(shè)計...
    艱苦奮斗的侯小憨閱讀 3,191評論 2 39
  • 第3章 基本概念 3.1 語法 3.2 關(guān)鍵字和保留字 3.3 變量 3.4 數(shù)據(jù)類型 5種簡單數(shù)據(jù)類型:Unde...
    RickCole閱讀 5,505評論 0 21
  • 今天講行動學習。其實工作和生活中很多時候用到行動學習。只要用于解決問題,只要是兩人以上,通過群策群力的方法,將散...
    Catheay閱讀 276評論 0 0
  • 睡前聽葉清的《歧照.信得》,是很久沒聽了,很久了,從來沒有一個男人的讀書像葉清這樣不矯揉造作,不偏不倚,好像...
    張迦男閱讀 376評論 0 1
  • classKnowViewModel:NSObject{ //被觀察的屬性必須用dynamic修飾 dynamic...
    _鋒閱讀 421評論 0 0

友情鏈接更多精彩內(nèi)容