如何在没有 eval() 的情况下做到这一点

发布于 2024-09-13 05:50:42 字数 981 浏览 3 评论 0原文

for (var i in variables) {
    eval('var ' + i + ' = variables[i]');
}

基本上,我想将变量属性传输到局部变量。

是否有使用 eval()

或者其中哪个更好的替代方法:

1.

var _ = variables;
for (var i = 0; i < 100000; i++) {
    _.test1();
    _.test2();
    _.test3();
}

2 .

with (variables) {
    for (var i = 0; i < 100000; i++) {
        test1();
        test2();
        test3();
    }
}

3.

var test1 = variables.test1,
    test2 = variables.test2,
    test3 = variables.test3;
for (var i = 0; i < 100000; i++) {
    test1();
    test2();
    test3();
}

4.

for (var i in variables) eval('var ' + i + ' = variables[i]');
for (var i = 0; i < 100000; i++) {
    test1();
    test2();
    test3();
}
for (var i in variables) {
    eval('var ' + i + ' = variables[i]');
}

Basically, I want to transfer variables properties to local variables.

Is there an alternative to using eval()

Or which of these is better:

1.

var _ = variables;
for (var i = 0; i < 100000; i++) {
    _.test1();
    _.test2();
    _.test3();
}

2.

with (variables) {
    for (var i = 0; i < 100000; i++) {
        test1();
        test2();
        test3();
    }
}

3.

var test1 = variables.test1,
    test2 = variables.test2,
    test3 = variables.test3;
for (var i = 0; i < 100000; i++) {
    test1();
    test2();
    test3();
}

4.

for (var i in variables) eval('var ' + i + ' = variables[i]');
for (var i = 0; i < 100000; i++) {
    test1();
    test2();
    test3();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

舞袖。长 2024-09-20 05:50:42

通过查看您的评论似乎您的专业担心的是必须多次引用深度嵌套的对象,避免 eval 和 with ,我只是建议您使用别名标识符,例如:

 // some local scope...
 var foo = namespace.constructors.blah.dramatic.variables;
 // replace foo with something meaningful :)
 foo.method1();
 foo.method2();
 foo.property1;
 // etc...

这样深度嵌套的对象对象已经被解析,并且引用您的别名会更快,在这种情况下,evalwith IMO只会给您带来更多的问题而不是好处。

By looking at your comment seems that your major concern is having to reference several times a deeply nested object, avoiding eval and with I would simply recommend you to use an alias identifier, for example:

 // some local scope...
 var foo = namespace.constructors.blah.dramatic.variables;
 // replace foo with something meaningful :)
 foo.method1();
 foo.method2();
 foo.property1;
 // etc...

In that way the deeply nested object will already be resolved and referencing your alias will be faster, eval and with IMO would only cause you more problems than benefits in this case.

风情万种。 2024-09-20 05:50:42

一种替代方法是使该对象成为当前范围。它不会使属性成为局部变量,但您可以使用 this 关键字访问它们:

var variables = { a:42, b:1337 };

(function(){
  alert(this.a);
  alert(this.b);
}).apply(variables);

这样做的优点是您不会在任何地方复制任何内容,而是直接访问属性。

One alternative is to make the object the current scope. It won't make the properties local variables, but you can access them using the this keyword:

var variables = { a:42, b:1337 };

(function(){
  alert(this.a);
  alert(this.b);
}).apply(variables);

This has the advantage that you are not copying anything anywhere, you are accessing the properties directly.

温馨耳语 2024-09-20 05:50:42

好吧,我自己发布答案。

不能。

如果不知道变量的名称,不使用 eval() 就无法设置局部变量

……但是使用局部变量(选项 3)是最好的办法。

Well, I'll post the answer myself.

No.

There is no way to set local variables without knowing the name of the variable without using eval()...

...But using local variables (option 3) is the best way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文