VBScript and JavaScript event delegation Scheme

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.