Functions with variable arguments in javascript / jQuery

I need some advice.

This is my problem, I have the "N" function.

 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){    
        }
    });
};

all of these arr functions are placed in the object that is this.

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

after I wait for all Ajax functions to be executed with and after that, I'm fine.

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

    });

my problem is here:

function (a1, a2, ...an)

I want to have an object instead of the arguments of the function, because I do not know how many functions there will be.

So, I can edit the function object, which is cool $.when.apply($, fucArr), the problem is using variable numbers of arguments.

PS: Maybe I can use "apply" or "call" for these arguments?

Can someone give me an idea here. Thanks a lot of guys !!!

+5
source share
1 answer

, , arguments, :

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

apply :

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

All Articles