我必须在 Node.js 中创建一个应用程序来执行算法(在 js 中)并用它们的结果创建图表。
我的问题是我真的不知道如何在我的 textarea 中执行 JavaScript,我没有找到可以在 node.js 中运行 javascript 代码的插件。
谢谢
最佳答案
您可以在大多数 JS 方言中使用 JSON.eval("some string of javascript"),它将运行其中的代码。在此处查看更多信息:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval
很明显,这适用于学校,但作为一般做法,不推荐使用 JSON.eval(),因为它是运行某人想要的任何代码的一个很好的攻击向量。但是,无论您将其插入何处,它都可以在 Node JS 或浏览器的客户端上运行。只要它是用于学习练习,它可能就是您所需要的,但不要去找工作并到处使用 .eval() 。
这是我在 developer.mozilla.org 上链接到的那个页面的警告的复制粘贴
Don't use eval needlessly! eval() is a dangerous function, which executes the code it's passed with the privileges of the caller. If you run eval() with a string that could be affected by a malicious party, you may end up running malicious code on the user's machine with the permissions of your webpage / extension. More importantly, third party code can see the scope in which eval() was invoked, which can lead to possible attacks in ways to which the similar Function is not susceptible.
关于javascript - 从 TextArea 运行 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42543524/