for ... in and for ... of difference

for ... of ES6 as new traversal, allowing iterator traversing a data structure containing the interface and the return values, and for ... in ES3 the differences are as follows
1-for ... of traversing the object obtained is a bond value, for ... in the object are acquired keys
2-for ... in the chain traverses the entire prototype object, the performance difference is not recommended, but only for ... of the current object does not traverse the prototype chain traversal
3- array for traverse, for ... in the array will return all enumerated attributes (including attributes enumerated prototype chain), for ... of the array to return only the attribute value corresponding to the index

for ... of the loop is actually using the principle of internal traverse the object iterator interface for ... of the cycle be broken down into the most primitive of the for loop, the internal implementation of the mechanism can be understood
Here Insert Picture Description
can see that as long as the second condition is satisfied (iterator.next () and presence res.done is true) can always cycle continues and the next each time the method generates an iterator object assigned to res, res and assigning attributes to the value for ... of the first condition is declared variables can be, done res property controls whether to continue to traverse down
for ... of loop supports break, continue, return (called function words) and can be used with the deconstruction of the object and assign
Here Insert Picture Description
arr array each use for ... of circulation return of an object ({a: 1}, { a: 2}, {a: 3}), and then goes through the object deconstruction, to find the value of a property, assigned to obj.a, so when each cycle of obj. It will be assigned to a 1,2,3 respectively

Guess you like

Origin blog.csdn.net/smlljet/article/details/91523163