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.
Hi William!
ReplyDeleteI think there should be many different ways of solving something! It's important to be able to do it in different ways, because without variation, we wouldn't be unique and our community would be completely homogeneous. By having the choice of doing it in different ways, variety is introduced. However, in the world of computer programming, there does tend to be a right and wrong way. Often times, even when there is a specific right and wrong way, there are still a variety of other ways of doing things, and maybe one of those ways is better, but nobody has discovered it yet. So I believe it's still important to do things in many ways, even when programming!
Hi Josh, Thanks for reading my post and taking the time to comment! Programming can encompass a wide variety of peoples and personalities, which makes it a unique experience.I definitely agree with your statement that there should be multiple ways to do something, especially in the programming world, where there are nearly infinite possibilities!
Delete