2012-02-14 26 views
5

Ho bisogno e consigli.Funzioni con argomenti variabili in javascript/jQuery

Questo è il mio problema, ho le funzioni "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){  
     } 
    }); 
}; 

tutte queste funzioni arr inserite in un oggetto che è questo.

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

dopo che sto aspettando quando tutte le funzioni Ajax sono finite e dopo che sto bene.

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

    }); 

il mio problema è qui:

function (a1, a2, ...an) 

voglio avere invece gli argomenti delle funzioni di un oggetto perché non so quanti la funzione sta per essere.

Quindi posso modificare l'oggetto funzione, che è bello $.when.apply($, fucArr), il problema è utilizzare numeri variabili di argomenti.

PS: Forse posso usare "apply" o "call" anche per questi argomenti?

Qualcuno può darmi un'idea qui. Grazie mille ragazzi !!!

+0

possibile duplicato del [Permettere funzione javascript per accettare qualsiasi numero di argomenti] (http://stackoverflow.com/questions/3583044/allowing-javascript-function-to-accept-any-number-of-arguments) e [molti altri] (http: // stackoverflow. com/search? q = numero javascript + variabile + + di + argomenti). –

risposta

11

È possibile accedere a tutti gli argomenti passati a un metodo che utilizza the arguments keyword esempio:

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

The apply method può essere utilizzato per usare questi argomenti in un'altra funzione chiamata:

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

Blimey che è utile :) – CompanyDroneFromSector7G

Problemi correlati