如何在JavaScript中将函数作为参数传递 [英] How do I pass function as a parameter in javascript
问题描述
我有一个弹出式对话框,我正在尽可能使它变成动态的,所以我可以发送任何我想要的功能。我想创建一个回调函数,这样我就可以传递我想要的任何函数,并且可以在我的对象上执行所需的任何操作(在此示例中只是打印某些内容以便测试它。)
这是当弹出窗口被调用时会发生什么:
函数DisplayPopUp(obj,callback){
//单击
$('#actionButton')。click(function(e){
e.preventDefault();
callback(Obj);
});
}
以下是激活PopUp函数
$('。delete')。click(function(){
var obj =something something ;
DisplayPopUp(obj,function(){console.log('going to delete');});
});
不知何故,这是行不通的,我从萤火虫中得到这个错误:
Obj未定义
显然,我不会将我的功能转移到正确的位置 - 我应该怎么做你打电话给 callback(Obj)
,但是你的电话号码是变量名称是 obj
。检查你的情况。
I have a pop up dialog box and I am trying to make it as dynamic as I can so I can send to it any function I want. I wanted to create a callback function so I can pass any function I want and it will do whatever I need on my object (in this example just print something for testing it.
here is what happens when the pop up is being called:
function DisplayPopUp(obj, callback) {
//On Action Clicked
$('#actionButton').click(function(e){
e.preventDefault();
callback(Obj);
});
}
And here is the function that activates the PopUp function
$('.delete').click(function(){
var obj="something something";
DisplayPopUp(obj,function(){console.log('going to delete');});
});
Somehow that doesn't work and I get from firebug this error:
Obj is not defined
Clearly I do not transfer my function right - how should I do it?
You're calling callback(Obj)
but your variable name is obj
. Check your case.
这篇关于如何在JavaScript中将函数作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!