[英]passing an anonymous function to another function in Javascript
我了解您可以像這樣將另一個函數作為參數傳遞給另一個函數
var fn = function(){alert('Hello')}
function outer(a,fn){
fn();
}
從外部函數獲取參數后,如何將匿名函數傳遞給另一個函數並在函數中調用它?
function outer(function(x){alert(x);})
{
var xVar = "foo";
//..would liked to pass xVar to the anaonymous function
//passed as a param to the function so that "foo" is displayed as message...
}
請注意,更改外部的簽名將是最后的選擇。
您將函數調用(調用函數)與函數聲明(定義函數)混淆了。 這是您的要求:
// declare the outer function
function outer(func)
{
var xVar = 'foo';
func(xVar)
}
// now invoke it
outer(function (x)
{
alert(x);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.