在Javascript中将字符串(是一个函数)转换回函数 [英] Convert string (was a function) back to function in Javascript
问题描述
我在下面将此功能作为字符串。我如何将其转换回函数?我从JQuery事件中提取事件处理程序,我想将它们存储为字符串,然后将它们转换回来,因为它们将保存在mySQL中
I have this function below as a string. How would I convert it back into a function? I am pulling event handlers from JQuery events and I want to store them as string then convert them back because they will be saved in mySQL
function () {
if (!GActiveClick) {
return;
}
SaveProduct();
}
推荐答案
var func = new Function(theFunctionString);
func();
MDN :
new Function([arg1 [,arg2 [,... argN]],] functionBody)
new Function ([arg1[, arg2[, ... argN]],] functionBody)
参数
Parameters
arg1,arg2,... argN
函数用作正式参数名称的名称。每个必须是一个字符串,对应于有效的JavaScript标识符或用逗号分隔的此类字符串列表;例如x,theValue或a,b。
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
包含包含函数定义的JavaScript语句的字符串。
functionBody
A string containing the JavaScript statements comprising the function definition.
更新:
全部这是一个非常糟糕的做法!
你应该有一个泛型函数来获取构建你想要的参数。唯一改变的是参数。将这些参数存储在您拥有的数据库中。
All this a very bad practice!
You should have a generic function that get parameters that build what you want. The only thing that change are the parameters. Store those parameters in the DB you have.
这篇关于在Javascript中将字符串(是一个函数)转换回函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!