Domanda

I am using javascript eval(). For some reason, it is causing bugs, so I am replacing all eval. I have replaced this

var abc = "item." + str3 + ".power";
abc = eval(abc);

with

var abc = item[str3]["power"];

But I don't understand how do I replace these two statements?

1) setTimeout(eval(reloadfunction), 180000);

2) buttonrow = buttonrow + eval(button)(i, item);

reloadfunction is a variable which gets some string value which is a function name. button is a variable which gets some string value and executes it as function and passes "i" and "item" which are other variables.

È stato utile?

Soluzione

If the variables and functions you are dealing with are global (ie. defined in the global scope) then you can use window[reloadfunction] and window[button](i,item).

If they're locally scoped, however, you will have to completely restructure your code to have something like a map of functions:

var functions = {
    func1: function() {doSomething();},
    func2: function() {doSomethingElse();}
};
setTimeout(functions[reloadfunction],180000);

Altri suggerimenti

I would use this :

setTimeout( new Function(reloadfunction) , 180000);

since it runs in its own scope. ( not global , not current but its own)

p.s. you could set context also :

new Function(reloadfunction).apply(t,[]) //immediate execute

new Function(reloadfunction).bind(t,[]) //future execute (notice ie=>9)

as for comment : here is an example :

enter image description here

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top