- Joined
- Jan 2, 2014
- Messages
- 1,787
- Reaction score
- 939
- First Language
- Chinese
- Primarily Uses
- N/A
This topic aims to incite you to share as many javascript ES5 coding mistakes as you know, in order to help all of us learn from those shared mistakes, or simply make fun of them 
Let's start by sharing mine:
Not knowing Javascript ES5 variables are references
Not knowing Javascript ES5 uses short circuit evaluation
Not knowing Javascript ES5 only has global and function scopes(corresponding to global and local variables)
Not knowing return only works for the innermost function
I'll continue to share more and more later. Let's share Javascript ES5 coding mistakes you know as well 
Let's start by sharing mine:
Not knowing Javascript ES5 variables are references
Example - Treating this:
// array1 and array2 refers to 2 different empty arraysarray1 = [];array2 = [];As the same as this:
array1 = array2 = []; // Both array1 and array2 refer to the same empty arrayHowever, this:
// Both array2 and array3 refer to what array1 currently refers toarray2 = array1;array3 = array1;Is the same as this:
array2 = array3 = array1; // Both array2 and array3 refer to what array1 currently refers toApplication - Thinking that this method's referentially transparent:
// This function actually isn't side effect freesideEffectFreeFunction = function(array, val) var tempArray = array; // The passed argument array will be modified due to sort tempArray.sort(function(a, { return a.someProp - b.someProp; }); for (var index = 0, length = tempArray.length; index < length; index++) { if (tempArray[index].someProp > val) { return tempArray[index]; } } return null;} // sideEffectFreeFunctionWhen a truly side effect free version should be something this :
// This function's really side effect freesideEffectFreeFunction = function(array, val) // tempArray is a new array instead of referring to what array currently refers to var tempArray = array.slice(0); // tempArray.sort(function(a, { return a.someProp - b.someProp; }); for (var index = 0, length = tempArray.length; index < length; index++) { if (tempArray[index].someProp > val) { return tempArray[index]; } } return null;} // sideEffectFreeFunction
// array1 and array2 refers to 2 different empty arraysarray1 = [];array2 = [];As the same as this:
array1 = array2 = []; // Both array1 and array2 refer to the same empty arrayHowever, this:
// Both array2 and array3 refer to what array1 currently refers toarray2 = array1;array3 = array1;Is the same as this:
array2 = array3 = array1; // Both array2 and array3 refer to what array1 currently refers toApplication - Thinking that this method's referentially transparent:
// This function actually isn't side effect freesideEffectFreeFunction = function(array, val) var tempArray = array; // The passed argument array will be modified due to sort tempArray.sort(function(a, { return a.someProp - b.someProp; }); for (var index = 0, length = tempArray.length; index < length; index++) { if (tempArray[index].someProp > val) { return tempArray[index]; } } return null;} // sideEffectFreeFunctionWhen a truly side effect free version should be something this :
// This function's really side effect freesideEffectFreeFunction = function(array, val) // tempArray is a new array instead of referring to what array currently refers to var tempArray = array.slice(0); // tempArray.sort(function(a, { return a.someProp - b.someProp; }); for (var index = 0, length = tempArray.length; index < length; index++) { if (tempArray[index].someProp > val) { return tempArray[index]; } } return null;} // sideEffectFreeFunction
Example - Treating this:
// expr1 will always be run but expr2 will only be run if expr1 is falsyif (expr1 || expr2) { expr3; }//As the same as this:
// expr2 will always be run but expr1 will only be run if expr1 is falsyif (expr2 || expr1) { expr3; }//Application - Writing buggy codes like this:
// exprThatShallAlwaysBeRun won't run if this._var1 is already truthythis._var1 = this._var1 || exprThatShallAlwaysBeRun;//When it should be something like this:
// Ensures exprThatShallAlwaysBeRun will indeed always be runvar1 = exprThatShallAlwaysBeRun;this._var1 = this._var1 || var1;//
// expr1 will always be run but expr2 will only be run if expr1 is falsyif (expr1 || expr2) { expr3; }//As the same as this:
// expr2 will always be run but expr1 will only be run if expr1 is falsyif (expr2 || expr1) { expr3; }//Application - Writing buggy codes like this:
// exprThatShallAlwaysBeRun won't run if this._var1 is already truthythis._var1 = this._var1 || exprThatShallAlwaysBeRun;//When it should be something like this:
// Ensures exprThatShallAlwaysBeRun will indeed always be runvar1 = exprThatShallAlwaysBeRun;this._var1 = this._var1 || var1;//
Example - Treating this:
someFunction = function(target) { var index = someOtherFunction(target); for (var index = 0; index < target; index++) { yetSomeOtherFunction(index, target); } // index is now target - 1 instead of someOtherFunction(target) target = someOtherFunctionAgain(); yetSomeOtherFunctionAgain(index, target);} // someFunctionAs the same as this:
someFunction = function(target) { var index = someOtherFunction(target); for (var i = 0; index < target; index++) { yetSomeOtherFunction(i, target); } target = someOtherFunctionAgain(); yetSomeOtherFunctionAgain(index, target);} // someFunction
someFunction = function(target) { var index = someOtherFunction(target); for (var index = 0; index < target; index++) { yetSomeOtherFunction(index, target); } // index is now target - 1 instead of someOtherFunction(target) target = someOtherFunctionAgain(); yetSomeOtherFunctionAgain(index, target);} // someFunctionAs the same as this:
someFunction = function(target) { var index = someOtherFunction(target); for (var i = 0; index < target; index++) { yetSomeOtherFunction(i, target); } target = someOtherFunctionAgain(); yetSomeOtherFunctionAgain(index, target);} // someFunction
Example - Treating this:
someFunction = function(array, val) var tempArray = array.slice(0); tempArray.sort(function(a, { return a.someProp - b.someProp; }); for (var index = 0, length = tempArray.length; index < length; index++) { if (tempArray[index].someProp > val) { return tempArray[index]; } } return null;} // someFunctionAs the same as this:
someFunction = function(array, val) var tempArray = array.slice(0); tempArray.sort(function(a, { return a.someProp - b.someProp; }); tempArray.forEach(function(element) { // return in forEach is the same as continue in for if (element.someProp > val) { return element; } } ); return null;} // someFunction
someFunction = function(array, val) var tempArray = array.slice(0); tempArray.sort(function(a, { return a.someProp - b.someProp; }); for (var index = 0, length = tempArray.length; index < length; index++) { if (tempArray[index].someProp > val) { return tempArray[index]; } } return null;} // someFunctionAs the same as this:
someFunction = function(array, val) var tempArray = array.slice(0); tempArray.sort(function(a, { return a.someProp - b.someProp; }); tempArray.forEach(function(element) { // return in forEach is the same as continue in for if (element.someProp > val) { return element; } } ); return null;} // someFunction
Last edited by a moderator:
