加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 建站 > 正文

JavaScript 的一些常用设计模式

发布时间:2019-08-20 01:04:03 所属栏目:建站 来源:xianshannan
导读:计划模式是前人办理某个特定场景下对而总结出来的一些办理方案。也许刚开始打仗编程还没有什么履历的时辰,会感受计划模式没那么好领略,这个也很正常。有些简朴的计划模式我们偶然辰用到,不外没意识到也是存在的。 进修计划模式,可以让我们在处理赏罚题目的

例子

  1. class A { 
  2.   getContent() { 
  3.     return '第一行内容' 
  4.   } 
  5.   render() { 
  6.     document.body.innerHTML = this.getContent() 
  7.   } 
  8.  
  9. function decoratorOne(cla) { 
  10.   const prevGetContent = cla.prototype.getContent 
  11.   cla.prototype.getContent = function() { 
  12.     return ` 
  13.       第一行之前的内容 
  14.       <br/> 
  15.       ${prevGetContent()} 
  16.     ` 
  17.   } 
  18.   return cla 
  19.  
  20. function decoratorTwo(cla) { 
  21.   const prevGetContent = cla.prototype.getContent 
  22.   cla.prototype.getContent = function() { 
  23.     return ` 
  24.       ${prevGetContent()} 
  25.       <br/> 
  26.       第二行内容 
  27.     ` 
  28.   } 
  29.   return cla 
  30.  
  31. const B = decoratorOne(A) 
  32. const C = decoratorTwo(B) 
  33. new C().render() 

计策模式

在计策模式(Strategy Pattern)中,一个举动或其算法可以在运行时变动。

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读