简体   繁体   English

就像在C#中一样,在JavaScript中添加回调

[英]Add callbacks in javascript just as in C#

In C# I can do the following: 在C#中,我可以执行以下操作:

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. 然后,当我调用c() ,将调用f1f2

How can I achieve the same result in javascript? 如何在javascript中达到相同的结果?

I mean I can do in javascript the following: 我的意思是我可以使用javascript执行以下操作:

var c;

function f1() {

}

function f2() {

}

c = f1;

But I can not add f2 to c . 但是我不能将f2添加到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. JavaScript没有C#委托的概念。

Using jQuery you can achieve something similar, but not the same: 使用jQuery,您可以实现相似但不相同的功能:

 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 这个Delegate类的行为与C#委托相似

It's called a multicast delegate and I'm not sure there's a direct way to represent this in JavaScript. 它被称为多播委托,我不确定是否有直接的方法可以用JavaScript表示。 But I found this article on implementing multicast delegates in JavaScript . 但是我发现这篇文章介绍了如何在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. 当您在对象上调用.apply()时,它将按添加它们的顺序调用所有函数。

 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(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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