0

I'm trying to add functions into an array. These have to be named 'opdracht1' through to 'opdracht10'. Though, I cannot figure out how to give it a name.

var opdrachtArray = [];

for (i = 0; i < 10; i++) { 
    opdrachtArray.push(function() {func(i); });
}

It adds the functions but as I said earlier I cannot find out how to add a name.

Also, am I later just able to define the functions and call them when I need them?

4
  • Why do they have to be named? Commented Nov 7, 2014 at 8:48
  • Because I'm going to use each function as a seperate assignment for what i'm making. This allows me to edit them seperately and keep them apart. Commented Nov 7, 2014 at 8:50
  • That smells like an XY problem... But may want to use a key/value object, instead of an array. Commented Nov 7, 2014 at 8:51
  • The functions have names, opdrachtArray[0 .. 9]. Commented Nov 7, 2014 at 9:17

4 Answers 4

1

Name your functions by placing them on the window object:

for (i = 0; i < 10; i++) { 
    f = function() { func(i); }); // but see below
    window['opdracht' + i] = f
    opdrachtArray.push(f);
}

However you have a more basic problem. All your functions close over i and therefore func is always going to be called with the value of i after the loop finishes, in other words, 10. One solution is:

function make_func(i) {
    return function() {
        return func(i); 
    };
}

then

for (i = 0; i < 10; i++) { 
    f = make_func(i);
    window['opdracht' + i] = f;
    opdrachtArray.push(f);
}

Or

for (i = 0; i < 10; i++) { 
    (function(i) {
        var f = function() { func(i); };
        window['opdracht' + i] = f
        opdrachtArray.push(f);
    }(i));
}

or just use func.bind(null, i), which does approximately the same thing.

for (i = 0; i < 10; i++) { 
    f = func.bind(null, i);
    window['opdracht' + i] = f;
    opdrachtArray.push(f);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Your code seems to give an error. "Uncaught TypeError: undefined is not a function"
@OriginDutch: Cannot reproduce that error and can't imagine where it would come from.
Much better after the edits; a [new/different] programmer can easily miss assignments in expressions. I'm still not sure if they OP needs the functions to be "named in window" though. This answer also does answer the "next" question the OP will have.
0

If you want each function to be assigned to a name, use dictionary, not array:

    var opdrachtDict = {};
    for (i = 0; i < 10; i++) { 
        opdrachtDict["opdracht"+i]=func.bind(null,i);
    }
    function func(i){
        alert(i);
    }

    opdrachtDict["opdracht3"](); //ok, lets call it and see if you think its still a wrong answer ;)...

4 Comments

"Unexptected Token [" @ line 3
Remove the dot after opdrachtArray, and you're done :)
"These have to be named 'opdracht1' through to 'opdracht10'." But here, they are not so named, but rather held in a dictionary with that as the key.
This accepted solution also does not fix an important bug in the OP's code, which is that all ten functions will call func with the value 10.
0

You could store the name and function in an object and push each object to the array:

var opdrachtArray = [];

for (i = 0; i < 10; i++) { 
    var name = 'opdracht' + (i+1);
    opdrachtArray.push({name : name, callback : function() {func(i); }});
}

Comments

0

Well a function defined in the global scope just ends up being a property of self/window anyways. i.e.:

function mr_function(){return 5;}
self["mr_function"];
window["mr_function"];

Both (property of self / window) reference the function we defined. I guess you could name your function that way if you're careful. Or assign them to some other object's property if you'd rather not make them global.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.