我正在尝试注册 JavaScript 函数/类上的事件,并试图使其尽可能接近下面的 C# 类。这有可能吗?
C#
public class MyClass
{
public event EventHandler evtMyEvent;
private void RaiseEvent()
{
if(evtMyEvent != null)
evtMyEvent(this, new EventArgs());
}
}
MyClass mc = new MyClass();
mc.evtMyEvent += (s,e) => { //Do Something };
JS
function MyClass()
{
// Add an event handler...
this.raiseEvent = function(){
// raise the event
}
}
var mc = new MyClass();
//register for the event