Gitlib Gitlib
首页
  • 分类
  • 标签
  • 归档
  • Golang开发实践万字总结
  • MySQL核心知识汇总
  • Redis实践总结
  • MQ实践万字总结
  • Docker数据持久化总结
  • Docker网络模式深度解读
  • 常用游戏反外挂技术总结
  • 读书笔记
  • 心情杂货
  • 行业杂谈
  • 友情链接
关于我
GitHub (opens new window)

Ravior

以梦为马,莫负韶华
首页
  • 分类
  • 标签
  • 归档
  • Golang开发实践万字总结
  • MySQL核心知识汇总
  • Redis实践总结
  • MQ实践万字总结
  • Docker数据持久化总结
  • Docker网络模式深度解读
  • 常用游戏反外挂技术总结
  • 读书笔记
  • 心情杂货
  • 行业杂谈
  • 友情链接
关于我
GitHub (opens new window)
  • PHP

  • Golang

  • Python

  • Javascript

    • PureMVC

      • PureMVC(JS版)源码解析:认识PureMVC
      • PureMVC(JS版)源码解析01:观察者模式解析
      • PureMVC(JS版)源码解析02:Notification类
      • PureMVC(JS版)源码解析03:Observer类
      • PureMVC(JS版)源码解析04:Notifier类
      • PureMVC(JS版)源码解析05:SimpleCommand类
      • PureMVC(JS版)源码解析06:MacroCommand类
      • PureMVC(JS版)源码解析07:Mediator类
      • PureMVC(JS版)源码解析08:Proxy类
      • PureMVC(JS版)源码解析09:View类
      • PureMVC(JS版)源码解析10:Controller类
      • PureMVC(JS版)源码解析11:Model类
      • PureMVC(JS版)源码解析12:Facade类
      • PureMVC(JS版)源码解析:总结
    • 从一个基础Javascript面试题谈起
    • Javascript垃圾回收机制
    • Javascript实现双向数据绑定
    • 浏览器渲染网页过程
    • 浏览器资源缓存机制总结
  • 其他语言

  • 编程语言
  • Javascript
  • PureMVC
Ravior
2013-10-06

PureMVC(JS版)源码解析03:Observer类

PureMVC

上篇文章,分析了Notification类(消息类),Notification(消息)是连接观察者(Observer)和通知者(Notifier)之间的桥梁。现在,从代码层面上讲一下Observer类。

通过源码,我们可以看出Observer类有两个属性,分别是notify和context,notify是函数类型,context是object类型,其中notify是Observer对象(观察者)接收到消息之后执行的回调函数,context是回调函数的执行作用域。

/**
 * The Observers callback Function
 * 
 * @private
 * @type {Function}
 */
Observer.prototype.notify= null;

/**
 * The Observers callback Object
 * @private
 * @type {Object}
 */
Observer.prototype.context= null;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

这两个属性,分别有get/set方法。[setNotifyMethod()/getNotifyMethod()/setNotifyContext()/getNotifyContext()];

Observer在构造函数中对这两个属性进行了初始化:

function Observer (notifyMethod, notifyContext)
{
    this.setNotifyMethod(notifyMethod);
    this.setNotifyContext(notifyContext);
};

1
2
3
4
5
6

此外,Observer类还有一个很重要的方法,就是Observer对象接收到消息执行的notifyObserver()方法:

/**
 * Notify the interested object.
 * 
 * @param {puremvc.Notification} notification
 *  The Notification to pass to the interested objects notification method
 * @return {void}
 */
Observer.prototype.notifyObserver= function (notification)
{
    this.getNotifyMethod().call(this.getNotifyContext(), notification);  
};

1
2
3
4
5
6
7
8
9
10
11
12

上面的代码的意思是在context(回调函数执行作用域)中调用notify()方法(回调函数)。

关于Observer类基本上就这几点了:

  • notify和context两个属性
  • 构造函数中对两个属性的初始化
  • notifyObserver()。

接下来的分析Notifier(通知者)类。

#PureMVC
上次更新: 2022/12/01, 11:09:34
PureMVC(JS版)源码解析02:Notification类
PureMVC(JS版)源码解析04:Notifier类

← PureMVC(JS版)源码解析02:Notification类 PureMVC(JS版)源码解析04:Notifier类→

最近更新
01
常用游戏反外挂技术总结
11-27
02
Golang开发实践万字总结
11-11
03
Redis万字总结
10-30
更多文章>
Theme by Vdoing | Copyright © 2011-2022 Ravior | 粤ICP备17060229号-3 | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式