Javascript version:
Function delegate ()
{
This. event = new array ();
}
Delegate. Prototype. Add = function (fun, OBJ)
{
This. Event [This. event. Length] = function (){
Fun. Apply (OBJ );
};
}
Delegate.prototype.exe c = function ()
{
For (VAR I = 0; I <this. event. length; I ++)
{
This. Event [I] ();
}
}
Delegate. Prototype. Del = function (Num)
{
If (Num <this. event. length)
{
This. event. splice (Num, 1 );
}
}
VBScript version:
Class delegate
Dim Arr ()
Dim arrtrue
Private sub class_initialize
Arrtrue = false
End sub
Function add (funname, OBJ)
If arrtrue = true then
Redim preserve Arr (ubound (ARR) + 1, 1)
Else
Arrtrue = true
Redim preserve Arr (0, 1)
End if
Set Arr (ubound (ARR), 0) = OBJ
Arr (ubound (ARR), 1) = funname
End Function
Sub Exec
For I = 0 to ubound (ARR)
Execute "Arr (" & I & ", 0)." & Arr (I, 1)
Next
End sub
End Class
The javascript version may be better understood, and vbs needs to use the reflection mechanism to simulate the delegation.
Execute "Arr (" & I & ", 0)." & Arr (I, 1) the key is this sentence to complete the closure in Js.