Created
August 29, 2018 12:33
-
-
Save jonniek/6ba271eb005e429a9dd4b499e94bf307 to your computer and use it in GitHub Desktop.
This file contains 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 iterableStack() { | |
let arr = [] | |
return { | |
push: (data) => arr.push(data), | |
pop: () => arr.pop(), | |
[Symbol.iterator]: function* () { | |
let index = 0 | |
while (arr[index]) { | |
yield arr[index] | |
index++ | |
} | |
}, | |
} | |
} | |
const stack = iterableStack() | |
stack.push(1) | |
stack.push(2) | |
stack.push(3) | |
stack.push(4) | |
stack.push(5) | |
console.log("pushed to stack values: 1, 2, 3, 4, 5") | |
console.log("popped from stack value:", stack.pop()) | |
console.log("iterating stack:") | |
for (let item of stack) { | |
console.log(" " + item) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment