Convert string to function in javascript

I did a little search, but this particular problem is a bit like "how I evaluate function names from strings", so it’s hard for me to find a solution, I want to convert the string to a function so to speak, I had something like:

for (var i = 0; i < someNumber ; i++) { var foo = "function() { someObject.someOtherFunctionCall(" + i + ") }"; someArray[i] = foo; } 

how would i draw foo so i can call

 someArray[0](); 

I need a value baked in a function, any ideas?

EDIT: I just replaced the "value" with the "i" apology for the confusion

UPDATE:

Ok, I accepted icktoofay's answer because it works to answer most of your questions and concerns; I tried most of the proposed methods, which either did not pass the scope variable of the calls or did not require closing, which were stored with the last value of the function variable, unfortunately, I do not control the rest of the code, so I can not make changes to the location that the function in ultimately causes.

This is probably a workaround; I know how ugly syntax lines are for functions. as far as browser compatibility is concerned, this will only work in one environment, so I think we're pretty safe there.

Anyway, thanks for the tips and discussion.

+4
source share
5 answers

If you only need an array of functions that just call the method, this is much simpler:

 for(var i=0; i<someNumber; i++) { someArray.push(someObject.someOtherFunctionCall.bind(someObject, i)); } 

Please note that this requires Function.bind , which may not be available in older browsers. If you need support for older browsers, you can unpin this or use this version:

 for(var i=0; i<someNumber; i++) { someArray.push(function(i) { return function() { return someObject.someOtherFunctionCall(i); }; }(i)); } 

Edit: Here is an old answer that should also work, but less elegant:

 for (var i = 0; i < someNumber ; i++) { var foo = new Function("someObject", "return function() { someObject.someOtherFunctionCall(" + value + ") }")(someObject); someArray[i] = foo; } 
+4
source

Try:

 var foo = function() { someObject.someOtherFunctionCall(value ) }; someArray[i] = foo; 

or

If foo is not a global variable, you can do this to pass the value:

  var foo = (function(value) { return function() { someObject.someOtherFunctionCall(value ) } }) ( value ); 
+2
source

you can use the inside only between the brackets and call them using the eval() function.

here is the description: http://www.w3schools.com/jsref/jsref_eval.asp

here is an example:

 for (var i = 0; i < someNumber ; i++) { var foo = "someObject.someOtherFunctionCall(" + value + ");"; someArray[i] = foo; } eval(someArray[0]); 
+1
source

Even if you get this job, it is rather unsafe if the value is not sanitized. I would just use an anonymous function (without strings), for example:

 var foo = function() { someObject.someOtherFunctionCall(value) }; 

Due to the volume in JS value , foo will be available later, if you do not change the value after this line, then it will read the changed value .

EDIT: When I carefully looked at this question, I realized that this is not working. Please give me time to review my answer.

UPDATE: If you really want to do this, consider other answers.

(JS gods will cry though ...)

+1
source

Just give it your own volume,

 for (var i = 0; i < someNumber ; i++) { someArray[i] = (function(idx){ return function() { someObject.someOtherFunctionCall(idx); }; })(i) } 
+1
source

Source: https://habr.com/ru/post/1382966/


All Articles