This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function colors(){ | |
| const colors = ['red', 'blue', 'green']; | |
| let colorIndex = -1; | |
| const iterator = { | |
| next(){ | |
| colorIndex++; | |
| colorIndex %= 3; | |
| return {value: colors[colorIndex], done: false}; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function range(from, to){ | |
| let currentValue = from; | |
| const iterator = { | |
| next(){ | |
| if (currentValue == to) { | |
| return {value: undefined, done: true}; | |
| } | |
| return {value: currentValue++, done: false}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const a = [1, 2, 3]; | |
| const itr = a[Symbol.iterator](); | |
| itr.next() // {value: 1, done: false} | |
| itr.next() // {value: 2, done: false} | |
| itr.next() // {value: 3, done: false} | |
| itr.next() // {value: undefined, done: true} | |
| itr.next() // {value: undefined, done: true} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const user = {id: 1, username: 'thanos', email: '[email protected]'}; | |
| const {email: userEmail, ...restUser} = user; | |
| console.log(userEmail, restUser) // "[email protected]" {id: 1, username: "thanos"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const user = {id: 1, username: 'thanos', email: '[email protected]'}; | |
| const {id: userId, username, email: userEmail, canFly = false} = user; | |
| const {canFly: canFly2, canFly: thanosCanFly = false} = user; | |
| console.log(canFly, canFly2, thanosCanFly) // false undefined false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const avengers = [ | |
| {name: 'Iron Man', canFly: true}, | |
| {name: 'Captain America', canFly: false}, | |
| {name: 'Spider Man', canFly: false}, | |
| {name: 'Thor', canFly: true}, | |
| {name: 'Ant Man', canFly: false}, | |
| ]; | |
| const [, {canFly: CaptainAmericaCanFly}, , {canFly: ThorCanFly}] = avengers; | |
| console.log(CaptainAmericaCanFly, ThorCanFly); // false true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const avengers = ['Captain America', 'Iron Man', 'Thor', 'Hulk', 'Spider Man', 'Ant Man'] | |
| const lastIndex = avengers.length - 1; | |
| const {[lastIndex]: theLastAvenger} = avengers; | |
| console.log(theLastAvenger) // Ant Man |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const a = [1, 2, 3, 4]; | |
| const {2: x, 3: z} = a; | |
| console.log(x, z) // 3 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const a = [1, 2, 3, 4]; | |
| const [, , z] = a; | |
| console.log(z) // 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const a = [1, 2, 3, 4]; | |
| const [x, y, z] = a; | |
| console.log(x, y, z) // 1 2 3 |