简体   繁体   中英

Add callbacks in javascript just as in C#

In C# I can do the following:

public delegate void Callback();

void f1() {

}

void f2() {

}

Callback c = f1;
c+=f2;

And then when I call c() I will f1 and f2 get called.

How can I achieve the same result in javascript?

I mean I can do in javascript the following:

var c;

function f1() {

}

function f2() {

}

c = f1;

But I can not add f2 to c .

var t = function(callback){
// do stuff
callback();
}

function callbackFuction(){
// this is your callback fucntion
}

function diffrentFuntion(){
   t(callbackFuction);
}

hope this anwsers your question

No, you can't. JavaScript does not have the concept of C# delegates.

Using jQuery you can achieve something similar, but not the same:

 var c = $({}); function a() { console.log('a called!'); } function b() { console.log('b called!'); } c.on('fire', a); c.on('fire', b); c.trigger('fire'); // calls both event handlers 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

If it suits you ...

Or you can implement it yourself, it is easy:

 function Delegate() { this.callbacks = []; } Delegate.prototype.add = function(fn) { if (typeof fn === 'function' && this.callbacks.indexOf(fn) === -1) { this.callbacks.push(fn); } }; Delegate.prototype.remove = function(fn) { var index = this.callbacks.indexOf(fn); this.callbacks.splice(index, 1); }; Delegate.prototype.trigger = function() { var args = arguments; this.callbacks.forEach(function(fn) { fn.apply(null, args); }); }; // so var d = new Delegate(); var f1 = function(arg) { console.log('f1', arg); }; var f2 = function(arg) { console.log('f2', arg); }; var f3 = function(arg) { console.log('f3', arg); }; d.add(f1); d.add(f2); d.add(f3); d.trigger('param'); // executes the three callbacks passing them the parameter // remove one of the callbacks; d.remove(f2); // add a repeated callback d.add(f1); d.trigger('again'); // only f1 and f3 are fired. 

this Delegate class acts as similarly to C# delegates as it is possible

It's called a multicast delegate and I'm not sure there's a direct way to represent this in JavaScript. But I found this article on implementing multicast delegates in JavaScript .

Here's another example. This may or may not meet your needs, but maybe you can modify it to do what you want.

Basically, you're creating an object that holds an array of function references. When you call .apply() on the object, it will call all the functions in the order you added them.

 function delegate(fn){ this.callbacks = []; this.add(fn); } delegate.prototype.add = function(fn){ if(typeof fn === "function"){ this.callbacks.push(fn); } } delegate.prototype.apply = function(){ for(var i in this.callbacks){ this.callbacks[i].apply(); } } // example // define callbacks function cb1() { console.log('callback 1'); } function cb2() { console.log('callback 2'); } // create delegate with first callback var callback = new delegate(cb1); // add second callback callback.add(cb2); // call delegates callback.apply(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM