• 发布订阅者模式


    发布订阅者模式

    概念以及DOM0和DOM2级

    设计模式思想:发布订阅

    单例设计模式、构造函数、promise设计模式……

    设计模式不是代码,是一种设计思想。有效地进行管理代码

    设计模式不是具体的某个知识点,是一种编程思想。

    发布订阅:我未来要做什么事情

    概念:在未来某一时刻我要去做很多事情,先把事情列出成计划清单,当时刻到来时一件一件按照清单执行。

    其实是跟dom2级事件学的。

    dom0级和2级的区别

    jQuery的发布订阅

    $.Callbacks

    基于ES6封装自己的发布订阅库

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>subscribe</title>
    6. </head>
    7. <body>
    8. <button id="btn">点击我</button>
    9. <script>
    10. (function(){
    11. class Subscribe{
    12. constructor(){
    13. this.pond=[]
    14. }
    15. add(func){
    16. // 类型校验
    17. if(typeof func !=='function') return;
    18. const isExist=this.pond.some(item=>item === func);
    19. if(!isExist){
    20. this.pond.push(func)
    21. }
    22. }
    23. remove(func){
    24. const index=this.pond.findIndex(item===func)
    25. if(index>-1){
    26. this.pond(index,1)
    27. }
    28. }
    29. fire(...args){
    30. this.pond.forEach(func=>func.call(this,...args))
    31. }
    32. }
    33. function _Subscribe() {
    34. return new Subscribe()
    35. }
    36. window._Subscribe=_Subscribe;
    37. // return function () {
    38. // return new Subscribe();
    39. // }
    40. })();
    41. const s1=new _Subscribe();
    42. function fn1(){
    43. console.log(1)
    44. }
    45. function fn2(){
    46. console.log(2)
    47. }
    48. function fn3(){
    49. console.log(3)
    50. }
    51. s1.add(fn1)
    52. s1.add(fn1)
    53. s1.add(fn2)
    54. s1.add(fn3)
    55. btn.addEventListener('click',function () {
    56. s1.fire()
    57. })
    58. </script>
    59. </body>
    60. </html>

    解决数组塌陷问题*

    在添加移除中,产生的经典问题:数组塌陷问题

    export default / import

    真实项目中的应用场景:

    某一时刻我要去做很多事情,先创建一个事件池,到那一时刻事件池通知执行;想做什么事情往事件池中加方法/移除方法,来管控整个业务逻辑。

    promise用异步解决

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>subscribe</title>
    6. </head>
    7. <body>
    8. <button id="btn">点击我</button>
    9. <script>
    10. (function(){
    11. class Subscribe{
    12. constructor(){
    13. this.pond=[]
    14. }
    15. add(func){
    16. // 类型校验
    17. if(typeof func !=='function') return;
    18. const isExist=this.pond.some(item=>item === func);
    19. if(!isExist){
    20. this.pond.push(func)
    21. }
    22. }
    23. remove(func){
    24. const index=this.pond.findIndex(item=>item===func)
    25. if(index>-1){
    26. // this.pond.splice(index,1)
    27. this.pond[index]=null
    28. }
    29. }
    30. fire(...args){
    31. // this.pond.forEach((func,index)=>{
    32. //
    33. // })
    34. const length=this.pond.length;
    35. for(let i=0;i<length;i++){
    36. const func=this.pond[i]
    37. if(typeof func !== 'function'){
    38. // 此时去移除
    39. this.pond.splice(i,1)
    40. i--;
    41. continue;
    42. }
    43. func.call(this,...args)
    44. }
    45. }
    46. }
    47. function _Subscribe() {
    48. return new Subscribe()
    49. }
    50. window._Subscribe=_Subscribe;
    51. // return function () {
    52. // return new Subscribe();
    53. // }
    54. })();
    55. const s1=new _Subscribe();
    56. function fn1(){
    57. console.log(1)
    58. }
    59. function fn2(){
    60. console.log(2)
    61. s1.remove(fn1)
    62. }
    63. function fn3(){
    64. console.log(3)
    65. }
    66. function fn4(){
    67. console.log(4)
    68. }
    69. s1.add(fn1)
    70. // s1.add(fn1)
    71. s1.add(fn2)
    72. s1.add(fn3)
    73. s1.add(fn4)
    74. btn.addEventListener('click',function () {
    75. s1.fire()
    76. })
    77. </script>
    78. </body>
    79. </html>

  • 相关阅读:
    配置Webpack Dev Server 实战操作方法步骤
    Java毕业设计源代码评教评分教务管理系统springboot
    一款免费轻量级web报表工具
    poj 1742 coins
    C++s简单实现Scoket编程
    人大与加拿大女王大学金融硕士——人生最好的贵人,就是努力向上的自己
    【web应用系统实践】第四章作业
    【业务知识】发票系统设计知识学习一
    我的阿里云盘资源搜索引擎首次试运行
    两种形式的import
  • 原文地址:https://blog.csdn.net/betterangela/article/details/128062562