This example describes the way JS uses eval () to create variables dynamically. Share to everyone for your reference, specific as follows:
One, what is the eval () function?
The Eval_r () function computes a string and executes the JavaScript code in it.
Ii. How to define variables dynamically?
Since Eval () is able to compute a string, why not convert the definition variable to a string, and then put it into the Eval_r () function to execute, as follows:
var definestr = "var number_" +i.tostring ();
Eval_r (DEFINESTR);
This defines a variable, and you can set the debugger trace to debug to see if the number variable is defined successfully.
If the I value is 1, then the dynamic variable is number_1
The following can be assigned a value in this form:
Eval_r ("Number_" +i.tostring () + "= 120;");
This assigns a value of 120 to its variable number_1.
But there is absolutely no way to write this:
Eval_r ("Number_" +i.tostring ()) = 120
This writing is in the wrong way.
This type of writing in IE is not workable, you need to use Executescript (string) to perform, judged as follows:
if (window.executescript)
{
//ie browser
window.executescript (DEFINESTR);
} else
{
//chrome, Firefox, etc. non ie browser
window.eval_r (DEFINESTR);
}
More about JavaScript content to view the site topics: "JavaScript Ajax Operating Skills Summary", "javascript json operation tips Summary", "JavaScript switching effects and techniques summary", " JavaScript Search Algorithm Skills Summary, "JavaScript animation effects and techniques Summary", "JavaScript Error and debugging skills Summary", "JavaScript data structure and algorithm skills summary", "JavaScript traversal algorithm and skills summary" and The summary of JavaScript mathematical operational usage
I hope this article will help you with JavaScript programming.