<script type="text/javascript">
var Delegate = function () {
this.array = [];
};
Delegate.prototype = {
constructor : Delegate
, add : function (a, b) {
this.array[this.array.length] = function () {
b.apply(a, Array.prototype.slice.call(arguments));
};
}
, del : function (num) {
if (num < this.array.length) this.array.splice(num, 1);
}
, exec : function () {
var arg = Array.prototype.slice.call(arguments), i = 0;
for (; i < this.array.length ;) this.array[i ++].apply(null, arg);
}
, detach : function () {
var i = 0;
for (; i < this.array.length ;) this.array[i ++] = null;
this.array = null;
}
};
var Class = function (n) {
this.name = n;
};
Class.prototype.showName = function () {
alert(this.name + "/n参数A:" + arguments[0] + "/n参数B:" + arguments[1]);
};
var delegateObj = new Delegate;
var a = new Class("a对象");
var b = new Class("b对象");
var c = new Class("c对象");
delegateObj.add(a, a.showName); //添加委托
delegateObj.add(c, c.showName);
delegateObj.add(b, b.showName);
delegateObj.del(1); //删除中间的即c
delegateObj.exec("第一个参数", "第二个参数"); //执行
delegateObj.detach();
delegateObj = null;
</script>