this series as effective JavaScript 's reading notes.
ECMAScript The standard does not provide for JavaScript of the Object The order in which attributes in the type are stored.
but in the use For : in looping through the properties of an Object , it is really necessary to rely on some sort of order. Because ECMAScript does not explicitly regulate this order, each JavaScript execution engine can be implemented according to its own characteristics, There is no guarantee of a for in a different execution environment . in loop behavior is consistent.
For example, the following code calls the Report The result of the method is indeterminate:
Function report (highscores) {var result = "", var i = 1;for (var name in highscores) {//unpredictable orderresult + = i + "." + Name + ":" +highscores[name "+" \ n "; i++;} return result;} Report ([{Name: "Hank", points:1110100},{Name: "Steve", points:1064500},{Name: "Billy", points:1050200}]);/?
If you do need to make sure that the results of the run are based on the order of the data, prioritize using the array type to represent the data, rather than directly using the Object type. Also, try to avoid using for . in loop, using an explicit for Loop:
Function report (highscores) {var result = ""; for (var i = 0, n = highscores.length; I < n; i++) {var score = Highscores[i];result + = (i + 1) + "." +score.name + ":" + score.points + "\ n";} return result;} Report ([{Name: "Hank", points:1110100},{Name: "Steve", points:1064500},{Name: "Billy", points:1050200}]);//"1." Hank:1110100\n2. Steve:1064500\n3. Billy:1050200\n "
Another behavior that is particularly dependent on order is the calculation of floating-point numbers:
var ratings = {"Good would hunting": 0.8, "Mystic River": 0.7, "$": 0.6, "doubt": 0.9};
in the Item 2 , it is mentioned that the addition of floating-point numbers does not even satisfy the commutative law:
(0.1 + 0.2) + 0.3 the results and 0.1 + (0.2 + 0.3) the results were
0.600000000000001 and the 0.6
Therefore, for the arithmetic operation of floating-point numbers, it is more impossible to use any order:
var total = 0, Count = 0;for (var key in ratings) {//unpredictable ordertotal + = ratings[key];count++;} Total/= count;total; // ?
when For : when the in traversal order is different, the resulting total results are different, and the following are the two calculation sequences and their corresponding results:
(0.8 + 0.7 + 0.6 +0.9)/4//0.75
(0.6 + 0.8 + 0.7 +0.9)/4//0.7499999999999999
of course, there is a solution to the problem of calculating floating-point numbers by using integer numbers, for example, we will enlarge the above floating-point number first Ten times into integer data, and then the calculation ends and then shrinks Ten Times:
(8+ 7 + 6 + 9)/4/10 //0.75
(6+ 8 + 7 + 9)/4/10 //0.75
Summarize:
- when using For : in loops, do not rely on the order in which they are traversed.
- when using Object type to hold the data, you need to ensure that the data in it is unordered.
- When you need to represent a collection with order, use the array type instead of the Object type.
Effective JavaScript Item 46 takes precedence using arrays instead of object types to represent ordered collections