js消息单利类简单封装


var NotificationCenter = (() => {
    var _Instance;
    function getInstance() {
        if (_Instance === undefined) {
            _Instance = new cc.EventTarget();
        }
        return _Instance;
    }
    /**
     * 注册消息通知
     * @param {消息key} NotificationName 
     * @param {消息回调} callBack 
     * @param { Object The target (this object) to invoke the callback, if it's not given, only callback without target will be removed} target 
     */
    function on(NotificationName, callBack, target) {
        this._Instance.on(NotificationName, callBack, target);
    }
    /**
     * 关闭消息通知
     * @param {消息key} NotificationName 
     * @param {消息回调} callBack 
     * @param { Object The target (this object) to invoke the callback, if it's not given, only callback without target will be removed} target 
     */
    function off(NotificationName, callBack, target) {
        this._Instance.off(NotificationName, callBack, target);
    }
    /**
     * 删除关闭 target 目标上的所有注册消息
     * @param {注册消息目标} target 
     */
    function targetOff(target) {
        this._Instance.targetOff(target);
    }
    /**
    * 注册消息通知,只调用一次的消息注册,调用玩直接删除
    * @param {消息key} NotificationName 
    * @param {消息回调} callBack 
    * @param { Object The target (this object) to invoke the callback, if it's not given, only callback without target will be removed} target 
    */
    function once(NotificationName, callBack, target) {
        this._Instance.once(NotificationName, callBack, target);
    }
    /**
     * 分发事件到事件流中
     * 冒泡事件,事件会从当前节点,向父节点之上冒泡传递
     * @param { Event The Event object that is dispatched into the event flow} event 
     */
    function dispatchEvent(event) {
        this._Instance.dispatchEvent(event);
    }
    /**
     * 发送消息通知
     * @param {消息key} NotificationName 
     * @param {消息传递参数} options 
     */
    function emit(NotificationName, options) {
        this._Instance.emit(NotificationName, options);
    }
    return {
        getInstance: getInstance,
    }
})();
module.exports = NotificationCenter;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值