我有一个主要的 post 函数,用于所有 post 调用:
function post_json(post_string, response_handler) {
//
// do logging and other things with post string
//
$.post(post_string,{},response_handler,'json');
}
然后我将在各个页面中使用此调用:
post_json(post_url, handler_generic);
handler_generic函数是这样的:
function handler_generic(json) {
var success = json.success;
var success_verbiage = json.success_verbiage;
// do lots of stuff here
}
这非常有效。
我想做的是有一个这样的功能:
function handler_generic_with_extra(json, unique_id) {
var success = json.success;
var success_verbiage = json.success_verbiage;
// do lots of stuff here and now do it with the
// unique id
}
我认为这些行不通,而且它们也行不通:
post_json(appended_post_string, handler_generic_with_extra( the_unique_id));
post_json(appended_post_string, handler_generic_with_extra( json, the_unique_id));
如果不创建新的 post_json 函数来处理这些情况,有什么办法可以实现这一点吗?
最佳答案
使用闭包:
post_json(appended_post_string,function(json){return handler_generic_with_extra( json, the_unique_id); });
关于javascript - 使用 $.post 并将返回函数作为带有附加参数的变量传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40898741/