0

update

solution works in foreach loop but not in for loop

function x(number){
  return number - 10;
}
var i = 0
var runtimefunctions = {};
var allLevels = {"1":"State","2":"Educational_Services","3":"Principal_Networks","4":"Schools"}
 for (var key in allLevels) {
   runtimefunctions[i] = function() { return x(i); };
i++;
};

console.log(runtimefunctions[1]()); // -6
console.log(runtimefunctions[2]()); // -6
console.log(runtimefunctions[3]()); // -6

tried hard to make functions but it's first time to create such thing so cant understand the proper way...

I have a function..

function x(number){
return number - 10;
}
runtimefunctions = {};
now I have a loop to run
[1,2,3].forEach(function(y){
   //here I want to create a function.. which will make a function x(y) -- like this
   runtimefunctions[x] = new Function("return function x_" + levelIterator + "(levelIterator){ console.log(levelIterator); x(" + y + ") }")();

});

so basically..want to make functions like this.

runtimefunctions= {
 "1": x(1),
 "2": x(2),
and so on
}
Luckyy
  • 1,021
  • 4
  • 15
  • 29
  • Are you saying that you have an array, and for each value int he array you want to call the function, passing that value as a param? – DMcCallum83 Oct 24 '17 at 15:29
  • yes right ...it can have any number so dynamic – Luckyy Oct 24 '17 at 15:33
  • 1
    You are over complicating things. Avoid using strings for later evaluations and, instead, assign the functions directly to their keys. – briosheje Oct 24 '17 at 15:34
  • Regarding your update: [JavaScript closure inside loops – simple practical example](https://stackoverflow.com/q/750486/218196) – Felix Kling Oct 24 '17 at 16:26

3 Answers3

6

Is this what you need?

function x(number){
  return number - 10;
}

var runtimefunctions = {};

[1,2,3].forEach(function(y){
   runtimefunctions[y] = function() { return x(y); };
});

console.log(runtimefunctions[1]()); // -9
console.log(runtimefunctions[2]()); // -8
console.log(runtimefunctions[3]()); // -7

To satisfy your next (for-in) requirement, you need to closure the index variable with additional function call:

var runtimefunctions = {}, i = 0;
var allLevels = {"1":"State","2":"Educational_Services","3":"Principal_Networks","4":"Schools"}
for (var key in allLevels) {
  runtimefunctions[i] = function(index){ return function() { return x(index); } }(i++);
};
dhilt
  • 18,707
  • 8
  • 70
  • 85
  • It worked like charm, but my fault. I had to use for loop for object instead of foreach loop..in forloop pls find updated question...all returns -6 – Luckyy Oct 24 '17 at 16:14
0

It is much easier. For example:

const createFunctionWith = (x) => {
    return (param) => console.log(x, param)
}
let a = [1,2,3].map(x => createFunctionWith(x));
console.log(a[1]("bebe")); // 2, "bebe"

https://jsfiddle.net/muLxoxLd/

karina
  • 789
  • 9
  • 26
0

You could do something like this

// Found in your code
var x = (a) => {
    console.log(a)
};    

var runtimefunctions = {};

[1, 2, 3].forEach(function(y) {
    //Create a function with a parameter named "levelIterator"
    runtimefunctions[y] = Function("levelIterator", "{ console.log(levelIterator); x(" + y + ") }");

});

runtimefunctions[1]('test')
TryingToImprove
  • 7,047
  • 4
  • 30
  • 39