日期:2014-05-16 浏览次数:20350 次
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script type="text/javascript"> var delegate1 = function() { //委托的方法列表 var ArrayFuction = []; //添加方法,相当于+= this.AddF = function(f) { if (typeof (f) === "function") { ArrayFuction.push(f); } else { throw new Error("委托只能接受方法"); } }; //移除方法,相当于-= this.RemoveF = function(f) { if (typeof (f) === "function") { var length = ArrayFuction.length; for (var i = 0; i < length; i++) { if (ArrayFuction[i] == f) { ArrayFuction = ArrayFuction.splice(i, 1); } } } else { throw new Error("委托只能接受方法"); } }; //调用委托下所有方法 this.Run = function() { for (var item in ArrayFuction) { ArrayFuction[item](); } } } var del = new delegate1(); function aa() { alert("添加进入了"); } function bb() { alert("添加2"); } del.AddF(aa); del.AddF(bb); del.RemoveF(1); del.Run(); </script> </head> <body> </body> </html>