简体   繁体   English

JavaScript ECMA-262重写eval函数

[英]JavaScript ECMA-262 re-write eval function

I am using a software tool (Opus Pro-Digital Workshop-UK) that uses OpusScript (a type of ECMA-262 JS). 我正在使用使用OpusScript(一种ECMA-262 JS)的软件工具(Opus Pro-Digital Workshop-UK)。 I've created an eval function, unsure how else to create the result. 我已经创建了一个eval函数,不确定如何再创建结果。 It works OK, but there are many online mentions of problems using eval functions. 它可以正常工作,但是在线上有很多使用eval函数的问题。 So, I am posting the function and hoping to get help re-writing it without eval, if this is possible: 因此,如果可能的话,我正在发布该函数,并希望获得帮助而无需eval来重新编写该函数:

//len = 36 //puzzle pieces-F1-F36
    function combiner10(len){
    s200 = new Array(214,401,607,791,977,1132,1140,1141,1142,1143,1146,1057,916,760,611,465,312,175,75,77,77,77,77,65,186,364,631,837,1035,1067,218,219,223,220,1158,1152);
    s300 = new Array(56,58,63,67,66,69,208,320,463,565,647,704,704,704,702,701,699,701,703,581,457,328,202,74,694,700,698,696,701,697,566,448,335,201,533,374);
    for (var i=1;i<=len;i++)
          eval("F"+i+".SetPosition(s200[i-1],s300[i-1])");
    }

I am a novice at scripting. 我是脚本新手。 Any help appreciated. 任何帮助表示赞赏。

I don't see a reason why you need to use eval() here at all. 我根本看不出您为什么需要在这里使用eval()的原因。

Put your pieces into an array so instead of variables named F1-F36, you simply use an array that contains each piece. 将您的片段放入一个数组中,因此您只需使用包含每个片段的数组来代替名为F1-F36的变量。 Then you can do this: 然后,您可以执行以下操作:

var pieces = [puzzle pieces F1-F36 in this array];   // F1 in the 0 position

function combiner10(len){
    var s200 = [214,401,607,791,977,1132,1140,1141,1142,1143,1146,1057,916,760,611,465,312,175,75,77,77,77,77,65,186,364,631,837,1035,1067,218,219,223,220,1158,1152];
    var s300 = [56,58,63,67,66,69,208,320,463,565,647,704,704,704,702,701,699,701,703,581,457,328,202,74,694,700,698,696,701,697,566,448,335,201,533,374];
    for (var i = 0; i < len; i++) {
          pieces[i].SetPosition(s200[i],s300[i]);
    }
}

Also, local variables should be declared with var in front of them. 同样,局部变量应该在它们前面声明为var

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

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