如何在javascript中将字符串转换为函数?

如何在javascript中将字符串转换为函数?,javascript,Javascript,我使用stringify将一个函数转换成字符串并存储到数据库中。 但是如何将这个字符串作为函数添加到变量中呢 假设我得到了字符串,就像我在 var A = "function aF() {\n console.log(\'change1\');\n}" 我想像这样将aF函数添加到对象关键点 {handle: A } 但我得到了这个结果 { handle: 'function aF() {\n console.log(\'change1\');\n }' } 相反,我想要这个

我使用stringify将一个函数转换成字符串并存储到数据库中。 但是如何将这个字符串作为函数添加到变量中呢

假设我得到了字符串,就像我在

var A = "function aF() {\n    console.log(\'change1\');\n}" 
我想像这样将aF函数添加到对象关键点

{handle: A }
但我得到了这个结果

{ handle: 'function aF() {\n    console.log(\'change1\');\n }' }
相反,我想要这个

{handle:[function: aF]} or {handle:[function]}

因为变量A是字符串的类型。有没有办法将函数转换为函数,然后存储为句柄键。

您可以使用函数构造函数生成函数

比如说

var A = "function aF() {\n    console.log(\'change1\');\n}" ;
var functionStr = A.substring(A.indexOf("{")+1, A.lastIndexOf("}"));
new Function(functionStr)();
注意:

使用此方法使用字符串创建
函数
对象与
eval()
一样危险。除非确定不涉及用户输入,否则不应执行此操作。如果用户输入用于生成
函数
字符串,则该函数不被认为是安全的,因为用户可能会对身份验证和授权进行操作,因为系统无法控制(验证)相同的操作

如果函数aF有一些参数呢

例如,您需要存储对函数对象的引用,并使用参数调用该引用

var A = "function aF() {\n    console.log(\'change1\');\n}" ;
var functionStr = A.substring(A.indexOf("{")+1, A.lastIndexOf("}"));
var functionObj = new Function(functionStr);
functionObj ( args );
例如,现在使用参数调用此函数

var A = "function aF() {\n    console.log(\'change1\');\n}" ;
var functionStr = A.substring(A.indexOf("{")+1, A.lastIndexOf("}"));
var functionObj = new Function(functionStr);
functionObj ( args );
或使用

或使用


另一个解决办法是:

var  A = "function aF() {\n    console.log(\'change1\');\n}" ;
eval(A);
{handle:aF}
你好

我写了一个小函数。 它非常简单,没有很多验证。 看这里

和测试

console.log(foo);
console.log(foo(1, 2));
console.log(foo(3, 4));
在jsfiddle上看到它
我会这样做,这几乎是对其他响应的另一种理解,但不涉及任何子字符串。代码上的注释几乎说明了一切

var yourExample = createFunction("function aF() {\n    console.log(\'change1\');\n}");
yourExample(); // prints change1 in console

var fnWithParam = createFunction("function aF(param1) { console.log(param1); }");
fnWithParam(2); // prints 2 in console


// creates a function from a string, that string must be a function itself.
function createFunction(fnStr) {
    // make a function which returns a function, unwrap that function by calling it with apply();
    return new Function('return ' + fnStr).apply();
}
此外,为了帮助减少对对象(如
窗口
文档
等)的访问,您可以在创建该函数的函数范围内创建新变量。例如:

// creates a function from a string, that string must be a function itself.
function createFunction(fnStr) {
    // make a function which returns a function, unwrap that function by calling it with apply();
    return new Function('"use strict"; var window,document; return ' + fnStr).apply();
}
现在,这并不能解决从字符串创建javascript的所有安全问题,但我认为这总比什么都没有好

良好阅读:


您也可以使用。最好重新考虑一下您正在尝试做什么,这样就不需要将代码存储为字符串。我不知道为什么人们对这个问题持否定态度。人们应该提供解决方案,而不是借口“不做”等等。@briosheje我无法解释为什么我需要这个,但我必须将函数作为字符串存储在数据库中。所以以后我可以用它。@briosheje是的,我知道这一点。但是如果你要做的项目是为了你的个人目的。数据库是100%安全的,只有个人开发者才会使用它,这就是为什么我不考虑安全性,也不想这么做的原因。@ScottMarcus:虽然这是真的,但如果你必须按照OP所描述的去做(将包含函数的字符串转换为实际函数),你必须这样做(或
eval
,或创建脚本标记,或类似问题。问题的缺陷不是答案的错。:-@gurvinder372如果函数aF有一些参数该怎么办。@Rishi:下面是一个eval+参数的示例。我不知道如何使用新的函数原型来实现这一点。老实说,看起来最接近您的解决方案就是使用这个。(另外,请下次在您的问题中澄清,您知道评估问题等;)
var yourExample = createFunction("function aF() {\n    console.log(\'change1\');\n}");
yourExample(); // prints change1 in console

var fnWithParam = createFunction("function aF(param1) { console.log(param1); }");
fnWithParam(2); // prints 2 in console


// creates a function from a string, that string must be a function itself.
function createFunction(fnStr) {
    // make a function which returns a function, unwrap that function by calling it with apply();
    return new Function('return ' + fnStr).apply();
}
// creates a function from a string, that string must be a function itself.
function createFunction(fnStr) {
    // make a function which returns a function, unwrap that function by calling it with apply();
    return new Function('"use strict"; var window,document; return ' + fnStr).apply();
}