[英]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. 要将数据传递给回调函数,只需使用正确的参数调用回调。
function doStuff( callback, data ){
callback( data );
}
doStuff( alert, 'a small dog' );
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 );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.