Home > Web Front-end > JS Tutorial > body text

Introduction to JS dynamic calling method name examples_javascript skills

WBOY
Release: 2016-05-16 17:08:31
Original
1625 people have browsed it

Let’s first look at a function of JS

JavaScript eval() function
Definition and usage The

eval() function can calculate a certain string and execute the JavaScript code in it.
Syntax

eval(string)

Parameter Description

string Required. A string to evaluate that contains a JavaScript expression to evaluate or a statement to execute.

Return value

The value obtained by evaluating string (if any).

Explanation

This method only accepts original string as parameter. If the string parameter is not an original string, then this method will return without any changes. Therefore please do not pass String objects as arguments to the eval() function.

ECMAScript implementations allow an EvalError exception to be thrown if an attempt is made to override the eval property or assign the eval() method to another property and call it through that property.

Throws

If there are no legal expressions and statements in the parameters, 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 Notes

Tip: Although the function of eval() is very powerful, it is rarely used in actual use.

Example

Example 1

In this example, we will apply eval() on several strings and see the returned results:

Copy code The code is as follows:



Output:

200
4
27

Example 2

Look at the results returned by eval() in other cases:
Copy code The code is as follows:

eval("2 3") // Returns 5
var myeval = eval; // An EvalError exception may be thrown
myeval(" 2 3"); // EvalError exception may be thrown

You can use the following code to check whether the parameters of eval() are legal:
Copy code The 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

The following is an example written by myself
Copy the code The 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 huge. Imagine that someone can call any of your methods by changing the name of the method where you call it.

The second method is mainly used as a self-defined method

Mainly because the second method requires a specific way to write
Copy the code The code is as follows:

function call(functionName) {
showmsgs["showmsg"]();
}

var showmsgs = { showmsg: function () {
alert("success");
}
}
call("showmsg");
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!