简体   繁体   English

如何将参数传递回匿名回调函数?

[英]How do I pass a parameter back to an anonymous callback function?

I'm confused about how this works. 我对这是如何工作感到困惑。 I'm not new to javascript and I have done this before, it was just a long time ago and I have since lost the code. 我不是javascript的新手,我以前做过这个,很久以前我已经丢失了代码。 I don't know what this scenario is referred to, as a search for the term callbacks doesn't seem to turn up any relevant information. 我不知道这个场景是指什么,因为搜索术语callbacks似乎没有出现任何相关信息。

1) What is this operation called? 1)这个操作叫什么? (ie: callback, passback, etc) (即:回调,回传等)
2) What do I do in doStuff() to pass a parameter back to the anonymous function? 2)我在doStuff()如何将参数传递回匿名函数?

Named Function/Object below 下面命名为Function / Object

function doStuff(param1,anonymousFunction){
   //what do I do here to pass a value to 'anonymousFunction()'?
   //Can I just declare a variable?
}

FunctionCall with anonymous function as parameter below FunctionCall使用匿名函数作为参数如下

doStuff('string', function(variableThatIWantToAccess){
   console.log(variableThatIWantToAccess);
});
function doStuff(param1,anonymousFunction){
   //what do I do here to pass a value to 'anonymousFunction()'?
   //Can I just declare a variable?

   anonymousFunction( param1 );
 }

To pass data to a callback function, simply call the callback with the correct arguments. 要将数据传递给回调函数,只需使用正确的参数调用回调。

Example

function doStuff( callback, data ){
  callback( data );
}

doStuff( alert, 'a small dog' );

working jsfiddle 工作jsfiddle

Edit 编辑

The comments show that OP was wondering about jQuery. 评论显示OP对jQuery感到疑惑。

An example of a jQuery-event-handler-like function. 类似jQuery事件处理程序的函数示例。

function listen( eventType, callback ){
  document.addEventListener( eventType, callback );
}

Well, just: 嗯,只是:

function doStuff(param1, anonymousFunction) {
    anonymousFunction(param1 + " parameter");
}

doStuff('string', function (variableThatIWantToAccess){
   console.log(variableThatIWantToAccess); // 'string paramter'
});

AKA callback AKA 回调

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

相关问题 通过回调将数据传递回匿名函数? - Pass data back to anonymous function via callback? 是否可以将参数传递给链接的匿名函数回调? - Is is possible to pass parameter to a chained anonymous function callback? 如何将参数传递给setTimeout()回调? - How do I pass a parameter to a setTimeout() callback? 如何将参数传递给JS函数而不将其包装在匿名函数中? - How do I pass a parameter into a JS function without wrap it in an anonymous function? 如何将按钮传递给onclick中的匿名回调函数? - How to pass button to anonymous callback function in onclick? 如何使用jquery将变量参数传递给匿名函数? - How can I pass a variable parameter to an anonymous function using jquery? JavaScript:如何将匿名函数作为函数参数传递? - JavaScript: How to pass an anonymous function as a function parameter? 如何将参数传递给 JavaScript 函数中的匿名函数? - How do I pass arguments to an anonymous function within a function in JavaScript? 如何在Javascript .filter()方法中将额外参数传递给回调函数? - How do I pass an extra parameter to the callback function in Javascript .filter() method? 如何将参数传递给自执行的匿名函数 - How to pass a parameter to a self executing anonymous function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM