Many JavaScript programmers have come across the EVAL command, as it has been around since JavaScript 1.0 and ECMA-262, but few will have used it. In this article, I'll take a quick look at several potential uses for this command, including one allowing us to write code that is easier to read, maintain, and has less lines while achieving the same effect. I will also look at how EVAL can assist you when you are using dynamic content in your Web-based applications.
The command executes the JavaScript contained within the brackets, in some cases this may return a value which can be assigned to a variable. But, how does this help JavaScript programmers in their day-to-day work? As EVAL allows you to dynamically create and execute JavaScript, you can use it to reduce the number of lines of code you need to use; you can also use it to create JavaScript to perform activities that would be significantly more difficult without using the command. In many cases, you can move this functionality out of a single page and convert it into a generic function that you can reuse thanks to the EVAL function.
You can use this command in a variety of activities, for example, if you have a list of fields' names which must contain a value on an HTML form, conventionally you would have had to write an if / then for each field, leading to verbose code, which is not the easiest to read, maintain, or debug, especially within a large and / or complex Web application (See Listing A). Using the EVAL command, you can reduce this to a simple array and loop as shown in this Listing B.
As you can see, you have reduced the size of the code required to check the eight fields from 32 to 10 with no loss of functionality, but with a savings of about 70 percent in terms the of number of lines required. Utilizing the EVAL command has also produced a much more user friendly, maintainable and concise code than the traditional approach.
The real power of this command is its ability to work with data that is not known at load time-for example, user-entered data. As I have mentioned, EVAL takes any string that you provide and executes it returning a result, if one is applicable. You can use this to execute JavaScript where what you are being asked to execute is not known at load time. For example if we have a calculation script that takes an equation provided by the user and returns a result, you can use EVAL to perform the calculation.
An example of the use of this command can be seen in the auto-generated function shown in Listing C from MacroMedia's DreamWeaver Editor.This code sets the location of the object specified by the targ parameter to that of the value attribute of the selObj drop- down; the other parameter simply resets the drop-down if set to true. The reason that EVAL is used here is that it allows the developer to provide the target for the function, which could be a variety of things, for example:
This is a free website blog template provided by TemplateMo. Feel free to download, modify and use this layout for any blog templates.