Is there a way in Javascript to have a delegate like the ones in c# ?
Example in c#
Object.onFunctionCall = delegate (vars v) {
Console.WriteLine("I can do something in this private delegate function");
};
I would like with my Javascript to have my main object do something over a long time, and shot a delegate once in a while to give a little update. All that without having to change the code itself of my class to adjust for the webpage.
function mainObject() {
this.onUpdate = function() { //Potentially the delegate function here
}
}
var a = new mainObject();
a.onUpdate = Delegate {
$(".myText").text("Just got a delegate update");
}
I dunno if it's clear enough.. havent found ressources on this so I suppose there is just no way to do so ?
NOTE: I am not looking into jquery Click delegates event here, but into delegating a function call like how it works in c#
Let me know
function ..
) are 'delegates' - that is, they are First Class Functions. You may also be interested in Function.bind, such that thethis
context is preserved (which is more C#-delegate-like). Also read through the related questions. – HysteroDelegate {
withfunction () {
. For instance, at the bottom: callinga.onUpdate()
would updated the text. If talking about events in C# (which are not delegates themselves) then that is a different concept. The example C# code shown won't "auto fire" .. not sure what is meant. – Hystero