我在某个函数的本地范围内定义了以下代码段:
let str = someString.replace(/(~\w+~)/g, match=>params[match](someValue));
{
"~idvP~": someValue=>{return someValue},
"~dtv~": ()=>{return Date.now()},
"~V~": ()=>{return someArray[someIndex(calculatedFromTheseValues)]}
}
该代码段在该特定函数中运行良好,成功地创建了对象,其中某些特定值为" someString"被片段大量取代。
然而,当我试图在另一个 - 几乎相同 - 功能的本地范围内使用完全相同的代码段时,我收到以下错误 - " params [匹配]不是函数":(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Not_a_function)
尽管出现了这样的错误消息,但是在片段行旁边的typeof params [match]会返回params [match]是一个函数,hm。
此外,我正在测试原始片段和原始参数的完整克隆是否可以在几乎完全相同的情况下运行#34;那些给我错误:结果是同样的错误,只是来自"在另一个地方成功"代码。
如果我使用eval()函数以下列方式重新执行不成功的代码段:
eval(params[match])
代替params[match](someValue)
表达式。即我得到以下代码段代替原始代码: let str = someString.replace(/(~\w+~)/g, match=>eval(params[match]));
"~dtv~": ()=>{return Date.now()};
我换成了
"~dtv~": "Date.now()"
然后在eval()下并且在params对象中使用值声明的这些更改,我的代码片段开始正常工作。只比我原来的片段慢。
关于什么可能导致 - 或不导致 - 的错误,如果是"没有eval()"片段?我到底在哪里?谢谢!
************* --------从这里添加请求的示例-------- ************ **
哇,我不完全确定完整的例子是否容易,但是请它,谢谢。这些代码段取自此链接可能找到的样本 - https://webequipage.com/,其中“DBEUV资产”和#39;按钮打开第二个屏幕,其中通过单选按钮可以评估在第一个屏幕上选择的特定属性的每个单独标题的值。这两个屏幕的值正在通过下面显示的函数准备(它们从3个嵌套for循环中调用)函数addNewAttributeTo_D()
和addNewTitleTo_D
使用eval()
,如果我尝试将其更改为变体without eval()
- 那么最后一个工作正常addNewValueTo_D()
{ {1}}函数,我得到提到的错误。
///////// Here it goes the sample code //////////
var valuePrototypeIn_D =
'{"_P":{"r":"","v":"~idvP~"},"a_i":{"r":"","v":"true"},"cb":{"r":"","v":"true"},"dtc":{"r":"","v":"~dtv~"},"dtd":{"r":"","v":""},"V":"~V~"}';
var titlePrototypeIn_D = // almost the same string as valuePrototype. It also includes valuePrototype
var attribPrototypeIn_D = // the same string as value and title Prototypes combined
function make_D(whoRequested) //keeps spare parts for all 3 functions below
{
switch (whoRequested) {
case "addNewAttributeTo_D": return {
"~idtD~": "_D.H.ID.v.replace(/[⟃⟄]/gu,'')",
"~_$tn~": "1",
"~_$vn~":"1",
"~ltn~": "1",
"~dta~": "Date.now()",
}
case "addNewTitleTo_D": return {
"~idtP~": "aNum",
"~_$vn~":"1",
"~lvn~": "1",
"~AT~": "$words[getRandomInt(1, $words.length)]",
"~dtt~": "Date.now()"
}
case "addNewValueTo_D": return {
"~idvP~": tNum=>{return tNum},
"~dtv~": ()=>{return Date.now()},
"~V~": ()=>{return $words[getRandomInt(1, $words.length)]}
}
break;
default: cAlert(`Nothing was found by function ${arguments.callee.name} on request of features from ${whoRequested}!`);
}
}
function addNewAttributeTo_D(aNum, tNum, vNum){// throws error "params[match] is not a function" if I try to change its version into "without eval()". Works fine with eval() though.
let params = make_D(arguments.callee.name);
let str = attribPrototypeIn_D.replace(/(~\w+~)/g,match=>eval(params[match]));
_D.aL[aNum] = JSON.parse(str);
}
function addNewTitleTo_D(aNum, tNum, vNum){// throws error params[match] is not a function if I try to change it into "without eval()". Works fine with eval too.
let params = make_D(arguments.callee.name);
let str = titlePrototypeIn_D.replace(/(~\w+~)/g,match=>eval(params[match]));
_D.aL[aNum][tNum] = JSON.parse(str);
_D.aL[aNum].ltn.v = tNum;
}
function addNewValueTo_D(aNum, tNum, vNum){ //!!! no eval() here, but works fine
let params = make_D(arguments.callee.name);
let str = valuePrototypeIn_D.replace(/(~\w+~)/g,match=>params[match](tNum) );
_D.aL[aNum][tNum][vNum] = JSON.parse(str);
_D.aL[aNum][tNum].lvn.v = vNum;
}