Iterator
What is the Iterator interface
JS represents a collection of objects mainly Array, Set, Object, Map, in the past, traversing they need to use two different methods, and now, JS proposed Iterator mechanism to provide a unified traversal methods to different data structures, that is, for ... of. In other words, only the deployment of a data Iterator to use for ... of traversal.
Iterator traversal is this:
- Create a pointer to an object, points to the start position of the current data structure. That is, on the object nature walker, is a pointer to the object.
- First call object pointer
next
method, the first pointer to a data structure member. - The second call object pointer
next
method, the pointer points to a data structure of the second member. - Frequently called object pointer
next
method, the end position until it points to the data structure.
Each call next
method, will return information about the current members of the data structure. Specifically, it is a return contains value
and done
the object two properties. Where value
property is the value of the current member, done
is a Boolean value that indicates whether the end of the traverse.
{ let arr = ['hello', 'world'] let map = arr[Symbol.iterator]() console.log(map.next()) // {value: "hello", done: false} console.log(map.next()) // {value: "world", done: false} console.log(map.next()) // {value: undefined, done: true} }
Native data structure can provide for ... of consumption
- Array
- Map
- Set
- String
- The TypedArray (a common fixed-length buffer type, allowing binary data read buffer)
- Function in the arguments object
- NodeList object
That is because the order of traversal object properties is uncertain, developers need to manually specify. Essentially, the walker is a linear process, for any non-linear data structure, the deployment interface is equivalent to traversing the deployment of a linear transformation .
Do the following treatment, the target for for ... of consumption:
{ // Custom Object iterator interfaces the let obj = { Start: [ . 1, 2,. 3 ], End: [ . 4,. 5,. 6 ], [Symbol.iterator] () { the let that = the this the let index = 0 the let ARR that.start.concat = (that.end) // arrays into the let len = arr.length return { Next () { // traversal IF (index < len) { return { value: ARR [index ++ ], DONE: to false // continue to loop } } the else { return { value: ARR [index ++ ], DONE: to true // terminate the loop } } } } } } // for verification of a the cycle can for (Key of the let obj) { the console.log (Key) // . 1 2. 5. 4. 3. 6 } }
Did not use a custom iterator Object Interface for of the loop is being given
{ let obj = { start: [1, 2, 3], end: [4, 5, 6], } for (let value of obj) { console.log(value) // TypeError: obj is not iterable } }
Occasions call Iterator interface
Deconstruction assignment
{ let set = new Set().add('a').add('b').add('c'); let [x,y] = set; console.log(set) // Set(3) {"a", "b", "c"} console.log(x) // a console.log(y) // b let [first, ...rest] = set; console.log(first) // a console.log(rest) // ['b','c'] }
Extended operator
{ // 例一 var str = 'hello'; console.log([...str]) // ['h','e','l','l','o'] // 例二 let arr = ['b', 'c']; console.log(['a', ...arr, 'd']) // ['a', 'b', 'c', 'd'] }