Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a string into function in javascript

I've done a bit of googling round but this particular problem is a little too similar to the "how do i evaluate function names from strings" so I'm having trouble finding a solution, I want to convert a string into a function so say i had something like:

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

how would I typecast foo so I could call

someArray[0]();

I need the value baked into the function, any ideas?

EDIT: I just substituted "value" for "i" apologies for the confusion

UPDATE:

Ok, I have accepted icktoofay answer because it works, to answer most of your questions and concerns; I have tried most of the methods suggested all of which either didn't pass the variable to the calling locations scope or required closures that persisted with the last functions variable value, unfortunately I don't have control over the rest of the code so I cant make modifications to the location the function eventually gets called.

This is probably a temporary solution; I'm aware of how ugly parsing strings for functions is. as far as browser compatibility goes this will only ever run in one environment, so I think we're pretty safe there.

Anyway, thanks again for the prompt answers and discussion.

like image 428
Waltzy Avatar asked Nov 24 '11 22:11

Waltzy


People also ask

How do you call a JavaScript function from a string?

There are two methods to call a function from string stored in a variable. The first one is by using the window object method and the second one is by using eval() method. The eval() method is older and it is deprecated.

What is f () in JavaScript?

The RegExp \f Metacharacter in JavaScript is used to find the form feed character (form feed is a page-breaking ASCII control character). If it is found it returns the position else it returns -1.

What does eval () do in JavaScript?

The eval() function evaluates JavaScript code represented as a string and returns its completion value. The source is parsed as a script.

Can we convert string value into number in JavaScript?

In JavaScript parseInt() function (or a method) is used to convert the passed in string parameter or value to an integer value itself. This function returns an integer of base which is specified in second argument of parseInt() function.


2 Answers

If you just want an array of functions that simply call a method, it's much more simple:

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

Note that this requires Function.bind, which may not be available in older browsers. If you need support for older browsers, you can shim it 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 the old answer, which should also work, but is less elegant:

for (var i = 0; i < someNumber ; i++) {
    var foo = new Function("someObject", "return function() { someObject.someOtherFunctionCall(" + value + ") }")(someObject);
    someArray[i] = foo;
}
like image 163
icktoofay Avatar answered Sep 21 '22 12:09

icktoofay


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 );  
like image 39
The Mask Avatar answered Sep 18 '22 12:09

The Mask