Recently we've defined a list of most common Javascript mistakes that developers make. These cover a wide variety of topics and I'm sure you will find among them at least one that you've committed yourself. We describe the theory behind each of the problems/bad practices and show concrete solution(s).
Do you think we're missing something obvious? Leave a comment and let us know about it.
1. Usage of for..in to iterate Arrays
Example:
var myArray = [ “a”, “b”, “c” ];
var totalElements = myArray.length;
for (var i = 0; i < totalElements; i++) {
console.log(myArray[i]);
}
The main problem here is that the for..in statement does not guarantee the order. Effectively this means that you could obtain different results at different executions. Moreover, if someone augmented Array.prototype with some other custom functions, your loop will iterate over those functions as well as the original array items.
Solution: always use regular for loops to iterate arrays.
var myArray = [ “a”, “b”, “c” ];
for (var i=0; i<myArray.length; i++) {
console.log(myArray[i]);
}
2. Array dimensions
Example:
var myArray = new Array(10);
There are two different problems here. First, the developer is trying to create an array already containing 10 items, and it will create an array with 10 empty slots. However, if you try to get an array item, you will get ‘undefined’ as result. In other words, the effect is the same as if you did not reserved that memory space. There is no really good reason to predefine the array length.
The second problem is that the developer is creating an array using Array constructor. This is technically correct. However it’s slower than the literal notation.
Solution: Use literal notation to initialize arrays. Do not predefine array length.
var myArray = [];
3. Undefined properties
Example:
var myObject = {
someProperty: “value”,
someOtherProperty: undefined
}
Undefined properties (such as someOtherProperty in the above example) will create an element in the object with key 'someOtherProperty' and value 'undefined'. If you loop through your array checking the existence of an element the following 2 statements will both return 'undefined':
typeof myObject['someOtherProperty'] // undefined typeof myObject['unknownProperty'] // undefined
Solution: if you want to explicitly declare a uninitialized properties inside an object, mark them as null
var myObject = {
someProperty: “value”,
someOtherProperty: null
}
4. Misuse of Closures
Example:
function(a, b, c) {
var d = 10;
var element = document.getElementById(‘myID’);
element.onclick = (function(a, b, c, d) {
return function() {
alert (a + b + c + d);
}
})(a, b, c, d);
}
Here the developer is using two functions to pass the arguments a, b and c to the onclick handler. The double function is not needed only adding complexity to the code.
The variables a, b and c are already defined in the inner function because they are already declared as parameters in the main function. Any function inside the inner function will create a closure with all variables defined by main function. This includes ‘regular’ variables (like d) and arguments (like a, b and c). Thus It is not necessary to pass them again as parameters using a auto-executable function.
See JavaScript Closures FAQ for an awesome explanation about closures and contexts.
Solution: use closures to simplify your code
function (a, b, c) { var d = 10; var element = document.getElementById(‘myID’); element.onclick = function() { //a, b, and c come from the outer function arguments. //d come from the outer function variable declarations. //and all of them are in my closure alert (a + b + c + d);}; }Reference link: http://corporate.tuenti.com/en/dev/blog/top-13-javascript-mistakes
Không có nhận xét nào:
Đăng nhận xét