首页 >> js开发 >> JavaScript原生js实现的观察者和订阅者模式简单示例
JavaScript原生js实现的观察者和订阅者模式简单示例
发布时间: 2021年1月13日 | 浏览:
| 分类:js开发
本文实例讲述了原生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
}
}
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
}
}
参考:淘宝前端团队技术库感兴趣的朋友可以使用在线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程序设计有所帮助。
// 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程序设计有所帮助。
相关文章:
- js解决vue-router 切换tab标签关闭时缓存问题js大全
- jsvue中destroyed方法的使用说明js大全
- js在vue中created、mounted等方法使用小结js大全
- jseslint+prettier统一代码风格的实现方法js大全
- js解决Vue router-link绑定事件不生效的问题js大全
- jsvue 清空input标签 中file的值操作js大全
- js解决vue的touchStart事件及click事件冲突问题js大全
- js解决vue 给window添加和移除resize事件遇到的坑js大全
- js微信小程序动态评分展示/五角星展示/半颗星展示/自定义长度展示功能的实现js大全
- jsvue 弹出遮罩层样式实例js大全