我想將函數數組作爲參數傳遞給函數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