我想将函数数组作为参数传递给函数x,然后在函数x中执行它们。我也会以某种方式传递参数,但一些参数只在函数x中初始化。
传递函数数组作为参数,对于函数中的每个函数运行函数
一些功能包括了诸如:
_showData(data,type);
console.log(data);
$('#loading').remove();
这里有一个例子:
// Called somewhere else
runFunctions([$('.dashboard').remove, $('.screen-loading').remove]);
var runFunctions = function(functions){
// do some things
for (var i = 0; i < functions.length; i++){
functions[i]();
}
任何想法?
编辑: 对不起,我刚刚意识到程序不知道对象是什么,因为我用ajax调用来改变范围。
var runFunctions = function(functions){
$.ajax({
method: "POST",
url: "php/database.php",
dataType: "JSON",
data: {type:type},
success: function(data, type){
for (var i = 0; i < functions.length; i++){
functions[i]();
}
}
})
}
这个什么:
_accessDatabase(
function(onSuccess){
$('.dashboard').remove();
var type = 'home';
_showData(data,type); // it doesn't know what data is, how can I pass it through?
$('.screen-loading').remove();
}
);
var _accessDatabase = function(onSuccess){
$.ajax({
method: "POST",
url: "php/database.php",
dataType: "JSON",
data: {},
success: function(data){
onSuccess(data);
}
})
}
我想通过VAR数据到的onSuccess功能,我该怎么办呢?
解决了:
var _request_successful = function onSuccess (data){
console.log("running onSuccess");
$('.dashboard').remove();
var type = 'home';
_showData(data,type);
$('.screen-loading').remove();
}
_accessDatabase(_request_successful);
var _accessDatabase = function(onSuccess){
$.ajax({
method: "POST",
url: "php/database.php",
dataType: "JSON",
data: {},
success: function(data){
onSuccess(data);
}
})
}
我可能误解了你的答案,但不应该将参数以相反的方式传递给你的'call'函数吗? –
@FrankFajardo是的,我刚刚醒来...没有喝我的咖啡:( –
我得到一个错误:未捕获TypeError:obj [方法]不是函数 – ChickenFeet