2012-02-14 23 views
5

Potrzebuję i porady.Funkcje ze zmiennymi argumentami w javascript/jQuery

To jest mój problem, mam funkcje "N".

var FirstOne = function(){ 
    return $.ajax({ 
     type: "POST", 
     url: hrefUrl, 
     data: JSON2.stringify(option), 
     contentType: "application/json; charset=utf-8", 
     dataType: "json",   
     error: function(status){ 
     }, 
     success: function(data){  
     } 
    }); 
}; 

var SecondOne = function(){ 

    return $.ajax({ 
     type: "POST", 
     url: hrefUrl, 
     data: JSON2.stringify(option2), 
     contentType: "application/json; charset=utf-8", 
     dataType: "json",   
     error: function(status){ 
     }, 
     success: function(data){  
     } 
    }); 
}; 


............. 


var NOne = function(){ 

    return $.ajax({ 
     type: "POST", 
     url: hrefUrl, 
     data: JSON2.stringify(optionn), 
     contentType: "application/json; charset=utf-8", 
     dataType: "json",   
     error: function(status){ 
     }, 
     success: function(data){  
     } 
    }); 
}; 

wszystkich tych funkcji ARR popychany w obiekcie, który jest taki.

var funcObject= [FirstOne(), SecondOne(), ....... NOne() ]; 

po tym, jak czekam, gdy wszystkie funkcje Ajax są wykonywane i po tym jak się czuję.

$.when.apply($, funcObject).done(function (a1, a2, ...an) { 
//  ..... here already doesn't matter 

    }); 

mój problem jest tutaj:

function (a1, a2, ...an) 

chcę mieć zamiast argumentów funkcji obiektu, bo nie wiem ile funkcja będzie.

więc mogę edytować obiekt funkcji, który jest chłodny $.when.apply($, fucArr), problemem jest użycie zmiennej liczbie argumentów.

PS: Być może mogę użyć „Zastosuj” lub „call” dla tych argumentów, jak również?

Czy ktoś może dać mi pomysł tutaj. Dzięki dużo facetów !!!

+0

możliwe duplikat [Dopuszczenie funkcji javascript zaakceptować dowolną liczbę argumentów] (http://stackoverflow.com/questions/3583044/allowing-javascript-function-to-accept-any-number-of-arguments) i [wiele innych] (http: // stackoverflow. com/search? q = javascript + variable + number + of + arguments). –

Odpowiedz

11

można uzyskać dostęp do wszystkich argumentów przekazywanych do metody z użyciem the arguments keyword np

function() { 
    Console.log(arguments); //arguments is an array 
} 

The apply method mogą być stosowane do korzystania z tych argumentów w innym wywołania funkcji:

function() { 
    someFunction.apply(this, arguments); 
} 
+1

Blimey to przydatne :) – CompanyDroneFromSector7G

Powiązane problemy