Ale*_*lex 0 javascript jquery scope eval
我正在尝试删除此函数中的eval语句.我已经习惯了这种[无论]样式的替换,但在这种情况下它不起作用.看一看:
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);?
Run Code Online (Sandbox Code Playgroud)
在函数中,所有我正常的"eval scrubbing"选项都不起作用,例如,我无法使用:
return this[what]
return [what]
return app[what]
return new Function(what);
Run Code Online (Sandbox Code Playgroud)
当然,这不是一个需要评估的奇怪案例吗?.. ps我必须注意,我不能在App内部重新调整变量,因为它是巨大代码库的一部分.
这是摆弄的东西:
不幸的是,你运气不好; eval是唯一可以访问这样的变量的东西.下次,不要那样做:)
您可以通过保留data对象来开始迁移:
var App = (function(fw) {
var data = {
somevar1: "hello",
somevar2: "world"
};
this.get = function(what) {
return data[what];
};
});
Run Code Online (Sandbox Code Playgroud)
然后逐步在您看到的整个代码库中执行此操作.它不应该破坏任何东西.