Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
Storing functions and their parameters in an array to call later

I would like to store multiple functions and their parameters in an array that I will later loop over and execute. When putting the parens around the parameters, the function executes immediately and its array value becomes undefined. I would like to prevent this from happening and call the function (with its parameter(s)) at a later time. Here is an example of what I am trying to accomplish...

function getTableAssignments(callbacks) {
    $.ajax({
        method: "POST",
        datatype: "jsonp",
        url: base_url + "users/getTableAssignments/" + event_id
    }).done(function (data) {
        tables = data;
        if (callbacks.length > 0) {
            $.each(callbacks, function (i, v) {
                v();
            });
        }
    });
}
getTableAssignments([func1(param1, param2), func2, func3(param3)]);
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Storing the parameters is the problem. Here is how to store the function references:

getTableAssignments([func1, func2, func3]);

Here is how to bind the parameters to the functions prior to execution. This returns a new function. There are other ways to do it, but I think this would be the simplest.

var boundFunc1 = func1.bind(null, param1, param2);
var boundFunc3 = func3.bind(null, param3);

getTableAssignments([boundFunc2, func2, boundFunc3]);

You can also store your parameters in an object as arrays, and lookup those stored parameters with the function name, but that seems messy, so I won't expound on that method (and it would require you to change the calling to code the lookup the parameters).

If you haven't read up on bind, I'd recommend this video

over 4 years ago · Santiago Trujillo Report

0

You need to use function expressions:

getTableAssignments([
    function() {func1(param1, param2); },
    func2,
    function() {func3(param3); }
]);
over 4 years ago · Santiago Trujillo Report

0

I think arrow functions would be a slick way of doing this:

function func1 (param) {
	console.log("func1 : " + param);
}
function func2 () {
	console.log("func2");
}
function func3 (param1, param2) {
	console.log("func3 : " + param1 + ", " + param2);
}

function executeFunctions(funcs) {
	for (var i = 0, len = funcs.length; i < len; i++) {
		funcs[i]();
	}
}

favorateParam = "bob";
executeFunctions([
	()=>func1("dad"),
	()=>func2(),
	()=>func3("jane",favorateParam)
]);

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!