JavaScript is sometimes too liberal and gives too much liberty in writing code, he does not ask you to specify variables types or to end lines with semicolon. Sometimes this is good, but sometimes this is bad, especially when your code grows in size and it becomes difficult to observe the evolution of variables when the code is executed. So, it is good to follow some simple rules.
1. Always use "var"
In JavaScript you can initiate a variable everywhere, but you must know that if you don't use the "var" keyword this variable will be visible in the global context, this can be harmful for your application because it can override the value of a variable that was declared before in the global context, may be by another ".js" file, so it can make your application to not work or it will be working in an inappropriate way. So, it is highly recommended to always use the "var" keyword when you want to declare a variable, in ECMAScript 5 strict mode it is mandatory to use "var" on variable initialization.
2. Never use "with"
The "with" keyword helps to reduce the code written to call a long object reference, it forces the JS interpreter to search in the "with" block all references that can be related to the given object and use them, this can influence the performance and the comprehension of the code. To avoid "with" it is only needed to use a variable which will store the desired reference, this will not cause performanc penalty as "with" does, and is much more simple to be understood. In ECMAScript 5 strict mode it is forbidden to use "with" in your code construction.
var a = {
b: {
d: 0,
e: function () {
// Do something
},
f: function () {
// Do something else
}
},
c: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
}
// Do not write like this:
with (a.b) {
d = 1;
alert(e());
alert(f());
}
// Replace it by by:
var g = a.b;
g.d = 1;
alert(g.e());
alert(g.f());
May be in this example it's more simple to use "with", but it becomes difficult to understand what variable we need if some members of object "a" exist as separate variables somewhere else. On the other hand, the use of a variable to store the reference is more intuitive, but you have to write a bit more.
3. Don't even think about "eval"
The eval function can be used only in a narrow domain when programming in JavaScript, so, it must not be used by you, never. It forces the interpreter to pass again through the provided string to interpret its content, this process is slow, unsafe and may produce errors. "eval" has some relatives: "Function", "setTimeout", and "setInterval", they do some specific actions but in general they call "eval". It is recommended to use them as less as possible, for "setTimeout" and "setInterval" you can provide callback functions instead of strings.
As a conclusion, I recommend to use strict mode to avoid all kinds of mistakes and performance pitfalls, it will restrict some language semantics and will gently inform about changes made in this mode. Strict mode educates developers to write a more secure JavaScript code, it is sometimes faster then the normal mode thanks to some optimizations in the JS engines and it can coexist with non strict code, so it does not influence the execution of engines that does not support the strict mode.
No comments:
Post a Comment