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
}
4
  • 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? Commented Oct 24, 2017 at 15:29
  • yes right ...it can have any number so dynamic Commented Oct 24, 2017 at 15:33
  • 1
    You are over complicating things. Avoid using strings for later evaluations and, instead, assign the functions directly to their keys. Commented Oct 24, 2017 at 15:34
  • Regarding your update: JavaScript closure inside loops – simple practical example Commented Oct 24, 2017 at 16:26

3 Answers 3

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++);
};
Sign up to request clarification or add additional context in comments.

1 Comment

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
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/

Comments

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')

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.