Let's take a look at a JS function first
JavaScript eval() function
Definition and usage
The eval() function calculates a string and executes the JavaScript code inside it.
grammar
eval(string)
Parameter Description
string Required. The string to be calculated, which contains the JavaScript expression to be calculated or the statement to be executed.
Return value
The value obtained by calculating string (if any).
illustrate
This method only accepts the original string as arguments, and if the string parameter is not the original string, the method will return without any changes. Therefore, please do not pass a String object as an argument to the eval() function.
If you try to override the eval property or assign the eval() method to another property and call it through that property, the ECMAScript implementation allows an EvalError exception to be thrown.
Throw out
If there are no legal expressions and statements in the parameter, a SyntaxError exception is thrown.
If eval() is called illegally, an EvalError exception is thrown.
If the Javascript code passed to eval() generates an exception, eval() will pass the exception to the caller.
Tips and comments
Tip: Although eval() has very powerful functions, it is not often used in actual use.
Example
Example 1
In this example, we will apply eval() on several strings and see the returned result:
<script type="text/javascript">
eval("x=10;y=20;(x*y)")
(eval("2+2"))
var x=10
(eval(x+17))
</script>
Output:
200
4
27
Example 2
Look at the result returned by eval() in other cases:
eval("2+3") // Return 5
var myeval = eval; // EvalError exception may be thrown
myeval("2+3"); // EvalError exception may be thrown
You can use the following code to detect whether the parameter of eval() is legal:
try {
alert("Result:" + eval(prompt("Enter an expression:","")));
}
catch(exception) {
alert(exception);
}
The first method is to use eval in js
Below is an example written by myself
call("showmsg");
function call(functionName){
eval("this."+functionName+"()");
}
function showmsg(){
alert("success");
}
eval can automatically recognize the string you spliced as a method and call it.
But the disadvantages are also very big. Imagine that someone can call any method you call by changing the method name of the place you call.
The second method is mainly used as a method defined by yourself
The second method mainly requires a specific way to write
function call(functionName) {
showmsgs["showmsg"]();
}
var showmsgs = { showmsg: function () {
alert("success");
}
}
call("showmsg");
JavaScript eval() function
Definition and usage
The eval() function calculates a string and executes the JavaScript code inside it.
grammar
eval(string)
Parameter Description
string Required. The string to be calculated, which contains the JavaScript expression to be calculated or the statement to be executed.
Return value
The value obtained by calculating string (if any).
illustrate
This method only accepts the original string as arguments, and if the string parameter is not the original string, the method will return without any changes. Therefore, please do not pass a String object as an argument to the eval() function.
If you try to override the eval property or assign the eval() method to another property and call it through that property, the ECMAScript implementation allows an EvalError exception to be thrown.
Throw out
If there are no legal expressions and statements in the parameter, a SyntaxError exception is thrown.
If eval() is called illegally, an EvalError exception is thrown.
If the Javascript code passed to eval() generates an exception, eval() will pass the exception to the caller.
Tips and comments
Tip: Although eval() has very powerful functions, it is not often used in actual use.
Example
Example 1
In this example, we will apply eval() on several strings and see the returned result:
Copy the codeThe code is as follows:
<script type="text/javascript">
eval("x=10;y=20;(x*y)")
(eval("2+2"))
var x=10
(eval(x+17))
</script>
Output:
200
4
27
Example 2
Look at the result returned by eval() in other cases:
Copy the codeThe code is as follows:
eval("2+3") // Return 5
var myeval = eval; // EvalError exception may be thrown
myeval("2+3"); // EvalError exception may be thrown
You can use the following code to detect whether the parameter of eval() is legal:
Copy the codeThe code is as follows:
try {
alert("Result:" + eval(prompt("Enter an expression:","")));
}
catch(exception) {
alert(exception);
}
The first method is to use eval in js
Below is an example written by myself
Copy the codeThe code is as follows:
call("showmsg");
function call(functionName){
eval("this."+functionName+"()");
}
function showmsg(){
alert("success");
}
eval can automatically recognize the string you spliced as a method and call it.
But the disadvantages are also very big. Imagine that someone can call any method you call by changing the method name of the place you call.
The second method is mainly used as a method defined by yourself
The second method mainly requires a specific way to write
Copy the codeThe code is as follows:
function call(functionName) {
showmsgs["showmsg"]();
}
var showmsgs = { showmsg: function () {
alert("success");
}
}
call("showmsg");