简体   繁体   English

Javascript中如何将一个字符串转换成可执行的代码行?

[英]How do I convert a string into an executable line of code in Javascript?

I have the following bit of code我有以下代码

console.log("I am");

var x = "console.log('Alive!')";

Now I only want to use x to execute the code-string that is assigned to it - I may not even know the value of x for example but simply want to execute it whatever it maybe - is this possible?现在我只想使用x来执行分配给它的代码字符串——例如,我什至可能不知道 x 的值,但只想执行它——这可能吗?

eval()这会将字符串转换为 javascript 代码。

eval("console.log('Alive! Woo!')");

eval and new Function let you parse and execute JavaScript code from strings. evalnew Function让你从字符串解析和执行 JavaScript 代码。

In general, avoid executing code from strings.通常,避免从字符串执行代码。 And never execute code from strings where the strings are untrusted input (for instance, if you take input from user A, never use these to evaluate it in a session with user B).并且永远不要从字符串中执行代码,其中字符串是不受信任的输入(例如,如果您从用户 A 获取输入,则永远不要在与用户 B 的会话中使用这些来评估它)。

I see answers here pointing you at eval .我在这里看到答案指向你eval eval grants access to your local variables and such to the code, making it really powerful and really dangerous if you use it with untrusted input. eval授予对您的局部变量等代码的访问权限,如果您将它与不受信任的输入一起使用,它会变得非常强大且非常危险。

Where possible, avoid eval .在可能的情况下,避免eval You can easily avoid it in your case:在您的情况下,您可以轻松避免它:

For instance:例如:

 console.log("I am"); var x = "console.log('Alive!')"; new Function(x)();

That code creates a function whose body is the text from x , then immediately executes the function.该代码创建了一个函数,其主体是来自x的文本,然后立即执行该函数。

What you are looking for is eval() .您正在寻找的是eval() By passing a string to this function you will evaluate the string as JavaScript code and it will return whatever return-value the code in the string returns.通过将字符串传递给此函数,您将把字符串作为 JavaScript 代码求值,并且它将返回字符串中的代码返回的任何返回值。

Be aware when using this function though.使用此功能时请注意。 You do not want to evaluate any code you do not know is safe to execute.您不想评估任何您不知道可以安全执行的代码。 For example, running user-generated code could mess up whatever you are making.例如,运行用户生成的代码可能会弄乱您正在制作的任何内容。 While using this in JavaScript on a website this will probably only cause issues on the client-side and hence probably won't be much of a security threat, you would want to be VERY careful when evaluating code on for example a server side.在网站上的 JavaScript 中使用它时,这可能只会导致客户端出现问题,因此可能不会构成太大的安全威胁,在评估例如服务器端的代码时,您需要非常小心。

As have been hinted to in other posts here you probably want to make a function instead of an evaluated string if you are in control of the source code that is to be run.正如在此处的其他帖子中所暗示的那样,如果您可以控制要运行的源代码,则您可能想要创建一个函数而不是评估字符串。

What you are looking for is called a function :您正在寻找的称为函数

function x() {
    console.log('Alive!');
}

If x is already a string containing the code you could use eval(x) to execute it.如果x已经是包含代码的字符串,您可以使用eval(x)来执行它。 eval is evil though. eval是邪恶的。

var x = "console.log('Alive!')";
eval(x)

have the function mathchallenge (num) add up all the numbers from 1 to num for example if the input is 4 then your program should return 10 because 1+2+3+4=10.让函数 mathchallenge (num) 将从 1 到 num 的所有数字相加,例如,如果输入为 4,那么您的程序应该返回 10,因为 1+2+3+4=10。 for the test cases the parameter num will be any number from 1 to 1000对于测试用例,参数 num 将是 1 到 1000 之间的任何数字

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM