本文实例讲述了原生js实现的观察者和订阅者模式。分享给大家供大家参考,具体如下:观察者模式也叫 发布者-订阅者模式,发布者发布事件,订阅者监听事件并做出反应在传统的前端解耦方面,观察者模式作为比较常见一种设计模式,大量使用在各种框架类库的设计当中。核心代码:
// eventProxy.js
'use strict';
const eventProxy = {
onObj: {},
oneObj: {},
on: function(key, fn) {

if(this.onObj[key] === undefined) {

this.onObj[key] = [];

}


this.onObj[key].push(fn);
},
one: function(key, fn) {

if(this.oneObj[key] === undefined) {

this.oneObj[key] = [];

}


this.oneObj[key].push(fn);
},
off: function(key) {

this.onObj[key] = [];

this.oneObj[key] = [];
},
trigger: function() {

let key, args;

if(arguments.length == 0) {

return false;

}

key = arguments[0];

args = [].concat(Array.prototype.slice.call(arguments, 1));


if(this.onObj[key] !== undefined

&& this.onObj[key].length > 0) {

for(let i in this.onObj[key]) {

this.onObj[key][i].apply(null, args);

}

}

if(this.oneObj[key] !== undefined

&& this.oneObj[key].length > 0) {

for(let i in this.oneObj[key]) {

this.oneObj[key][i].apply(null, args);

this.oneObj[key][i] = undefined;

}

this.oneObj[key] = [];

}
}
};

export default eventProxy;


// eventProxy.js
'use strict';
const eventProxy = {
onObj: {},
oneObj: {},
on: function(key, fn) {

if(this.onObj[key] === undefined) {

this.onObj[key] = [];

}


this.onObj[key].push(fn);
},
one: function(key, fn) {

if(this.oneObj[key] === undefined) {

this.oneObj[key] = [];

}


this.oneObj[key].push(fn);
},
off: function(key) {

this.onObj[key] = [];

this.oneObj[key] = [];
},
trigger: function() {

let key, args;

if(arguments.length == 0) {

return false;

}

key = arguments[0];

args = [].concat(Array.prototype.slice.call(arguments, 1));


if(this.onObj[key] !== undefined

&& this.onObj[key].length > 0) {

for(let i in this.onObj[key]) {

this.onObj[key][i].apply(null, args);

}

}

if(this.oneObj[key] !== undefined

&& this.oneObj[key].length > 0) {

for(let i in this.oneObj[key]) {

this.oneObj[key][i].apply(null, args);

this.oneObj[key][i] = undefined;

}

this.oneObj[key] = [];

}
}
};

export default eventProxy;

使用:引入全局事件类,或通过配置webpack将事件类设置为全局变量
import { eventProxy } from '@/utils'

class Parent extends Component{
render() {

return (









);
}
}
// componentDidUpdate 与 render 方法与上例一致
class Child_1 extends Component{
componentDidMount() {

setTimeout(() => {

// 发布 msg 事件

console.log(eventProxy)

eventProxy.trigger('msg', 'end','lll');

}, 5000);
}
render() {

return (

我是组件一


)
}
}
// componentDidUpdate 方法与上例一致
class Child_2 extends Component{
state = {

msg: 'start'
};

componentDidMount() {

// 监听 msg 事件

eventProxy.on('msg', (msg,mm) => {

console.log(msg,mm)

this.setState({

msg:msg

});

});
}

render() {

return


child_2 component: {this.state.msg}




}
}


import { eventProxy } from '@/utils'

class Parent extends Component{
render() {

return (









);
}
}
// componentDidUpdate 与 render 方法与上例一致
class Child_1 extends Component{
componentDidMount() {

setTimeout(() => {

// 发布 msg 事件

console.log(eventProxy)

eventProxy.trigger('msg', 'end','lll');

}, 5000);
}
render() {

return (

我是组件一


)
}
}
// componentDidUpdate 方法与上例一致
class Child_2 extends Component{
state = {

msg: 'start'
};

componentDidMount() {

// 监听 msg 事件

eventProxy.on('msg', (msg,mm) => {

console.log(msg,mm)

this.setState({

msg:msg

});

});
}

render() {

return


child_2 component: {this.state.msg}




}
}

参考:淘宝前端团队技术库感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具:http://tools./code/HtmlJsRun测试上述代码运行效果。在线HTML/CSS/JavaScript代码运行工具在线HTML/CSS/JavaScript代码运行工具http://tools./code/HtmlJsRun关于JavaScript相关内容感兴趣的读者可查看本站专题:《javascript面向对象入门教程》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》javascript面向对象入门教程JavaScript错误与调试技巧总结JavaScript数据结构与算法技巧总结JavaScript遍历算法与技巧总结JavaScript数学运算用法总结希望本文所述对大家JavaScript程序设计有所帮助。