I’m trying to remove the eval statement in this function. I’m used to the this[whatever] style replacement but it doesn’t work out in this instance.
Have a look:
var App = (function(fw) {
var somevar1 = "hello";
var somevar2 = "world";
this.get = function(what) {
return eval(what);
}
});
var app = new App({some: "thing"});
// now for the use:
console.log(app.get("somevar1"),app);
In the function, all my normal “eval scrubbing” options are not working for instance, I cant use:
return this[what]
return [what]
return app[what]
return new Function(what);
surely this isn’t an odd case where eval is necessary? .. ps I have to note that I CANNOT rescope the variables inside App as it’s part of a huge codebase.
Here’s something to fiddle with:
Unfortunately, you’re out of luck;
evalis the only thing that can access variables like that. Next time, don’t do things that way 🙂You can start a migration by keeping a
dataobject:And just gradually do this across the entire codebase where you see it. It shouldn’t break anything.