Javascript 在json中传递函数并执行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3946958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pass function in json and execute
提问by amateur
Is there any way that I can pass a function as a json string (conversion with JSON.stringify), send it to another function, parse the json and then execute the function that was in the json? I am using jquery and javascript.
有什么方法可以将函数作为 json 字符串传递(使用 JSON.stringify 进行转换),将其发送到另一个函数,解析 json,然后执行 json 中的函数?我正在使用 jquery 和 javascript。
采纳答案by treeface
Here's a working example
这是一个工作示例
Basically, you have to be careful with this sort of thing. If you take an extant javascript function, turn it to a string, and eval it, you might run into function redeclaration issues. If you are simply taking a function string from the server and you want to run it, you can do as I did on that jsfiddle:
基本上,你必须小心这种事情。如果您使用现有的 javascript 函数,将其转换为字符串,然后对其进行评估,您可能会遇到函数重新声明问题。如果您只是从服务器获取一个函数字符串并想运行它,您可以像我在那个 jsfiddle 上做的那样:
Javascript
Javascript
var myFunc = "function test() {alert('test');}";
$(document).ready(function() {
var data = new Object();
data.func = myFunc;
var jsonVal = $.toJSON(data);
var newObj = $.evalJSON(jsonVal);
eval(newObj.func);
test();
});?
回答by EasierSaidThanDone
Yes, you can. There are tons of ways to do it.
是的你可以。有很多方法可以做到。
And there is no need to use the "evil" eval function (please yahoogle why it should be avoided) as pointed out here: http://javascript.about.com/library/bleval.htm
并且没有必要使用这里指出的“邪恶” eval 函数(请雅虎为什么应该避免它):http://javascript.about.com/library/bleval.htm
var tmpFunc = new Function(codeToRun);
tmpFunc();
Whether it was JSON at any stage should be irrelevant.
在任何阶段是否是 JSON 应该无关紧要。
回答by Skilldrick
Yes, you can convert a function to a string with it's toString()
method.
是的,您可以使用它的toString()
方法将函数转换为字符串。
Here's an exampleto show converting a function to a string and back to a function:
这是一个示例,用于显示将函数转换为字符串并返回到函数:
var myfunc = function () {
alert('It works!');
}
var as_string = myfunc.toString();
as_string = as_string.replace('It works', 'It really works');
var as_func = eval('(' + as_string + ')');
as_func();
回答by vadimk
take a look at the JSONfnplugin.
看看JSONfn插件。
http://www.eslinstructor.net/jsonfn/
http://www.eslinstructor.net/jsonfn/
it does exactly what you're asking for.
它完全符合您的要求。
-Vadim
-瓦迪姆
回答by Cristian Garcia
I created a fork of JSONfn which enables you to stringify and parse objects and their prototypes. In my basic tests it worked fine.
我创建了一个 JSONfn 的分支,它使您能够对对象及其原型进行字符串化和解析。在我的基本测试中,它运行良好。
回答by Bennybear
I've found it helpful to use the JavaScript call() function when working with functions in JSON files.
我发现在处理 JSON 文件中的函数时使用 JavaScript call() 函数很有帮助。
var returnData = theJsonData.theFunction.call();
console.log(returnData); // prints any return data
I hope that helps anyone that stops by!
希望对路过的人有所帮助!
回答by Darin Dimitrov
No, you cannot do this. Functions cannot be JSON serialized. Instead of converting the object into JSON you could directly pass it to the other function without calling JSON.stringify.
不,你不能这样做。函数不能被 JSON 序列化。您可以将对象直接传递给另一个函数而不调用 JSON.stringify,而不是将对象转换为 JSON。