我在下面将这个函数作为一个字符串。我如何将它转换回函数?我从 JQuery 事件中提取事件处理程序,我想将它们存储为字符串,然后将它们转换回来,因为它们将保存在 mySQL 中
function () {
if (!GActiveClick) {
return;
}
SaveProduct();
}
最佳答案
var func = new Function(theFunctionString);
func();
MDN :
new Function ([arg1[, arg2[, ... argN]],] functionBody)
Parameters
arg1, arg2, ... argN
Names to be used by the function as formal argument names. Each must be a string that corresponds to a valid JavaScript identifier or a list of such strings separated with a comma; for example "x", "theValue", or "a,b".functionBody
A string containing the JavaScript statements comprising the function definition.
更新:
所有这些都是非常的糟糕做法!
你应该有一个通用函数来获取构建你想要的东西的参数。唯一改变的是参数。将这些参数存储在您拥有的数据库中。
关于javascript - 将字符串(是一个函数)转换回 Javascript 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10901217/