JavaScript中的eval有哪些替代方案? [英] What are the Alternatives to eval in JavaScript?

查看:172
本文介绍了JavaScript中的eval有哪些替代方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些看起来像这样的代码:

I have a little bit of code that looks just like this:

function StrippedExample(i1, i2, i3, i4, i5, i6, i7, i8) {
    this.i = [];
    for (var i=1,j=0 ;i<9;i++) {
        var k = eval("i"+i);
        if (k > 0) {
            this.i[j++] = k;
        }
    }
}

FireBug Profiler声称第二长函数是eval(),占运行时间的近6%。

FireBug profiler claims that second longest function is eval(), taking up to nearly 6% of the run time.

每个人都说eval是EVIL (就像糟糕的那样)而且很慢(正如我所发现的那样),但我真的无法做任何其他事情 - 服务器只是将数据从数据库中提取出来并推送到浏览器。

Everyone says eval is EVIL (as in bad) and slow (as I have found), but I can't really do anything else - the server simply pulls the data out the database and pushes to the browser.

我有哪些替代方案?我也可以这样做我在服务器上做这个,但这只会把负担转移到链上。我无法更改数据库布局,因为所有内容都挂钩到这8个变量,这是一项艰巨的任务。

What alternatives do I have? I could do the same as I am doing here on the server but that just shifts the burden higher up the chain. I can't change the database layout since everything hooks into those 8 variables and is a massive undertaking.

推荐答案

function StrippedExample(i1, i2, i3, i4, i5, i6, i7, i8) {
    var args = [i1, i2, i3, i4, i5, i6, i7, i8]; // put values in an array
    this.i = [];
    for (var i=0,j=0 ;i<8;i++) { // now i goes from 0-7 also
        var k = args[i]; // get values out
        if (k > 0) {
            this.i[j++] = k;
        }
    }
}

以上代码可以简化此外,我只做了最小的改变,以摆脱 eval 。你可以摆脱 j ,例如:

The above code can be simplified further, I just made the minimal change to get rid of eval. You can get rid of j, for example:

function StrippedExample(i1, i2, i3, i4, i5, i6, i7, i8) {
    var args = [i1, i2, i3, i4, i5, i6, i7, i8];
    this.i = [];
    for (var i = 0; i < args.length; i++) {
        var k = args[i];
        if (k > 0) { this.i.push(k); }
    }
}

是等价的。或者,使用内置的参数对象(以避免在两个地方放置参数列表):

is equivalent. Or, to use the built-in arguments object (to avoid having your parameter list in two places):

function StrippedExample(i1, i2, i3, i4, i5, i6, i7, i8) {
    this.i = [];
    for (var i = 1; i < arguments.length; i++) {
        var k = arguments[i];
        if (k > 0) { this.i.push(k); }
    }
}

即使您没有过滤列表,你不想做像 this.i = arguments 这样的事情,因为 arguments 不是真正的数组;它有一个你不需要的 callee 属性,并且缺少在 i 中可能需要的一些数组方法。正如其他人所指出的,如果你想快速将 arguments 对象转换为数组,你可以使用这个表达式:

Even if you weren't filtering the list, you don't want to do something like this.i = arguments because arguments is not a real Array; it has a callee property that you don't need and is missing some array methods that you might need in i. As others have pointed out, if you want to quickly convert the arguments object into an array, you can do so with this expression:

Array.prototype.slice.call(arguments)

您可以使用它而不是上面的 var args = [i1,i2 ... 行。

You could use that instead of the var args = [i1, i2 ... lines above.

这篇关于JavaScript中的eval有哪些替代方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆