I have sometimes misused these functions before when I was writing code. At first it was really confusing why the code was not running, and I was really confused. At this time, I was just starting to learn about JavaScript's shorthand and its "with" statement. I initially thought it was a shortcut and a faster way to compile and write code, but I was proven wrong, as Crockford describes the with function "the with statement significantly slows down JavaScript processors because it frustrates the lexical binding of variable names" (Crockford 110). This means that it confuses JavaScript when you use it, thus leading to an unpredictable result.
![]() |
Using the "with" statement is highly discouraged |
Next, after learning about how the 'with' function messes up the clarity of the code, I tried to find a surefire way to write what I wanted without confusing JavaScript. What I wanted to do was simply state that the variable a equaled variable "b".
The result was exactly how I expected it to turn out, and involved a significantly lower of risk. It was able to properly execute the function with ease. What I learned was that there was always a better and safer way to do something, as shown below where I used more keystrokes than when I used the 'with' function.
Function without using 'with' |
Another confusing feature of JavaScript that I found was 'eval'. Eval, defined by Crockford, is a function that gives a string to the JavaScript compiler to execute a certain command (Crockford 110). Simply, eval is something used to evaluate a string as a JavaScript expression. It is often the most misused feature of JavaScript because of how much experience is required to correctly execute it. Even if one could employ the 'eval' function, it is still highly discouraged, simply for the clarity of the code, which 'eval' fogs up. For example:
Eval statement reducing the clarity of the code |
Question: What is your opinion on the way to solve something? Do you believe there should only be one way to do something, or should there be a diversity of solutions?
Works Cited:
Crockford, Douglas. JavaScript: The Good Parts. O'Reilly Media, Inc., 2008.