简体   繁体   English

是否可以提供自定义范围,将remove vars添加到eval函数?

[英]Is it possible to provide a custom scope add remove vars to eval function?

I'm trying to use eval() to evalute a mathematical string with variables and functions 我正在尝试使用eval()评估带有变量和函数的数学字符串

ex: algo = "1+len+customfunction(6)" 例如: algo = "1+len+customfunction(6)"

So i have data for len and the function for customFunction . 所以我有len数据和customFunction的函数。

They are obviously declared in different scope. 它们显然在不同的范围内声明。

I tried with something like 我尝试了类似的东西

process = function(vars, algo) {
 return (function() {
    algo = algo.toLowerCase();
    return eval(algo);
  }).call(vars);
};

I need to provide required functions and variables to eval . 我需要为eval提供所需的函数和变量 Items are in different scopes, how do i do that ? 项目在不同的范围内,我该怎么做?

Now I'm a bit lost and confused, is this even possible ? 现在我有点迷茫和困惑,这有可能吗? I think using eval('var'+vName+'='+value) would be ok for vars but not suitable for functions. 我认为使用eval('var'+vName+'='+value)对于vars来说是可以的,但不适用于函数。

EDIT : btw eval can be replaced with (new Function(algo))() http://moduscreate.com/javascript-performance-tips-tricks/ 编辑 :btw eval可以替换为(new Function(algo))() http://moduscreate.com/javascript-performance-tips-tricks/

i would move the required data to an object, which hold all required items. 我将所需的数据移动到一个对象,其中包含所有必需的项目。 access is possible with any given string. 任何给定的字符串都可以访问。 this solution does not requires eval() 此解决方案不需要eval()

var data = {
    customfunction: function (x) {
        return Math.PI * x * x;
    },
    len: 5
};
var variable = 'len';
if (data[variable]) {
    // do something
    ;
}
var fn = 'customFunction';
function evaluate(vars, algo) {
    if (data[algo.toLowerCase()]) { // 
        return data[algo.toLowerCase()].call(vars);
    } else {
        // fallback
    }
}
var process = evaluate(vars, algo);

OK here is what i've found : 好的,这就是我所发现的:

  • we can't provide a context for eval 我们无法提供评估的背景
  • eval('1+1') can be replaced by ((new Function('1+1'))() and it's way faster eval('1 + 1')可以被((new Function('1 + 1'))()取代,并且速度更快
  • eval have different scopes, can't use eval('var a = 1'); eval具有不同的作用域,不能使用eval('var a = 1'); eval('alert(a)'); eval('alert(a)');

So, i'v managed to : 因此,我设法:

  • create a string to initiate all vars : vars += 'var '+key+'='+JSON.stringify(value)+';' 创建一个字符串来初始化所有vars:vars + ='var'+ key +'='+ JSON.stringify(value)+';''
  • replace at runtime my algo string functions with their full path : 在运行时用完整路径替换我的算法字符串函数:

    var algo = 'f(x)+5*2', x = 5;

    window.object.functions.x = function(a) { return a*2; };

    So at runtime i replace x( with object.functions.x( the code executed by eval finally is eval('object.functions.x(5)+5*2'); and output is 20 of course :) 所以在运行时我将x(替换为object.functions.x(最终由eval执行的代码是eval('object.functions.x(5)+5*2');输出当然是20 :)

It seems to work. 它似乎有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM