Ruby's Louvre

每天学习一点点算法

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

一个支持优先级的自定义事件系统

在AS3中有一个叫IEventDispatcher 的类,与浏览器的window对象,document或元素节点一样,提供了 addEventListener, removeEventListener, dispatchEvent等接口。相对于浏览器的addEventListener,Flash的参数是更丰富,其中有一个允许我们指定回调的优先级,让我们在fire时优先执行它们。这是一个非常好的东西,JS想实现它也不能,于是便有以下这个类。以后,你想你的其他组件拥有自定义事件功能,那么继承它就行了。

以下是源码,使用AMD的形式包装。你们用时,可以使用我的加载器或requireJS加载它就行了。或者干脆把它的外层去掉!

define("events", function(){
    //与node.js events模块同名,返回EventTarget类
    var EventTarget = function(target) {
        this._listeners = {};
        this._eventTarget = target || this;
    }
    EventTarget.prototype = {
        constructor: EventTarget,
        addEventListener: function(type, callback, scope, priority) {
            if(isFinite( scope )){
                priority = scope
                scope = null;
            }
            priority = priority || 0;
            var list = this._listeners[type],  index = 0, listener, i;
            if (list == null) {
                this._listeners[type] = list = [];
            }
            i = list.length;
            while (--i > -1) {
                listener = list[i];
                if (listener.callback === callback) {
                    list.splice(i, 1);
                } else if (index === 0 && listener.priority < priority) {
                    index = i + 1;
                }
            }
            list.splice(index, 0, {
                callback: callback,
                scope:    scope,
                priority: priority
            });
        },
        removeEventListener: function(type, callback) {
            var list = this._listeners[type], i;
            if (list) {
                i = list.length;
                while (--i > -1) {
                    if (list[i].callback === callback) {
                        list.splice(i, 1);
                        return;
                    }
                }
            }
        },
        dispatchEvent: function(type) {
            var list = this._listeners[type];
            if (list) {
                var target = this._eventTarget,  args = Array.apply([], arguments),i = list.length,  listener
                while (--i > -1) {
                    listener = list[i];
                    target = listener.scope || target;
                    args[ 0 ] = {
                        type:  type,
                        target: target
                    }
                    listener.callback.apply(target, args);
                }
            }
        }
    }
    return EventTarget;
})

现在我把它放到github中,一切以github的为准!

如果您觉得此文有帮助,可以打赏点钱给我支付宝1669866773@qq.com ,或扫描二维码

posted on   司徒正美  阅读(2196)  评论(0编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2009-12-07 (转)Google Closure: 糟糕的JavaScript
2009-12-07 tabIndex属性
点击右上角即可分享
微信分享提示