How can I create a new event/ like in c# in javascript?
private event EventHandler asdCompleted;
private void SetEventHandlers()
{
this.asdCompleted += asd_completed;
}
private void asd_completed(object sender, EventArgs e)
{
}
and fire the event anywhere like in c#:
this.asdCompleted(this, null);
you can define a simple delegate list, like the one used internally by .NET, as follows
function createEvent() {
var invokeList = [];
var event = function() {
for (var i = 0; i < invokeList.length; i++) {
invokeList[i].apply(arguments);
}
}
event.add = function(value) {
invokeList[invokeList.length] = value;
}
return event;
}
var foo = {
myEvent: createEvent()
}
foo.myEvent.add(function() { console.log('in my event'); });
foo.myEvent.add(function() { console.log('also in my event'); });
foo.myEvent();
If you use jQuery, then the answer is jQuery Callbacks
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With