dess中,一个简单的多路委托的实现

SDelegate设计与应用
本文介绍了SDelegate的设计原理及其在DOM事件派发等特定场景的应用。SDelegate通过封装函数调用,实现事件委托的复合与解除,提高了代码的灵活性与复用性。
var SDelegate = function(f, b, c) {
    if (b) {
        this.asFunction_ = function() {
            return f.apply(b, arguments);
        }
    } else {
        this.asFunction_ = function() {
            return f.apply(this, arguments);
        }
    }
    this.method_ = f;
    this.binding_ = b;
    this.continus = c;
}
SDelegate.composite = function(d) {
    if (d.continus) {
        var con = d.continus.composited_ = SDelegate.composite(d.continus);
        var method = d.asFunction_;
        return function() {
            con.apply(this, arguments);
            return method.apply(this, arguments);
        }
    } else {
        return d.asFunction_;
    }
}
SDelegate.prototype.call = function() {
    if (!this.composited_) this.composited_ = SDelegate.composite(this);
    return this.composited_.apply(arguments[0], Array.prototype.slice.call(arguments, 1));
}
SDelegate.prototype.remove = function() {
    var removeP = function(parent, item, test, data) {
        if (!item) return;
        parent.composited_ = item.composited_ = null;
        if (test(item, data)) {
            parent.continus = item.continus;
            removeP(parent, item.continus, test, data);
        } else {
            removeP(item, item.continus, test, data);
        }
    };
    return function(test, data) {
        var p = this;
        if (test(this, data)) {
            p = this.continus;
        }
        removeP(p, p.continus, test, data);
        p.composited_ = null;
        return p;
    }
}();
SDelegate.prototype.append = function(f, b) {
    return new SDelegate(f, b, this);
}

这个SDelegate用起来可能会比较诡异,比如很多操作都要重新赋值。Dess中,SDelegate主要用于一些特定场合,如DOM事件派发。

转载于:https://www.cnblogs.com/infinte/archive/2010/07/19/SDelegate-von-dess.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值