最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Storing functions and their parameters in an array to call later - Stack Overflow

programmeradmin1浏览0评论

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 bees 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 acplish...

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)]);

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 bees 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 acplish...

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)]);
Share Improve this question edited Nov 20, 2019 at 14:21 Wang Liang 4,4447 gold badges26 silver badges50 bronze badges asked Mar 15, 2017 at 20:05 user7531085user7531085 2
  • tables is for fun? – Roko C. Buljan Commented Mar 15, 2017 at 20:07
  • yes, global var i didn't list in my example – user7531085 Commented Mar 15, 2017 at 20:07
Add a ment  | 

3 Answers 3

Reset to default 3

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 remend this video

You need to use function expressions:

getTableAssignments([
    function() {func1(param1, param2); },
    func2,
    function() {func3(param3); }
]);

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)
]);

发布评论

评论列表(0)

  1. 暂无评论