Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created May 14, 2017 20:37
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save thinkphp/b6cdf9bed4567e2008a7f85eab2d756c to your computer and use it in GitHub Desktop.
simple stack implementation in js
const stack = (() => {
let arr = [],
index = -1;
const obj = {
push(val) {return arr[index += 1] = val},
pop() {
const val = arr[index];
arr[index] = undefined;
if (index >= 0) index -= 1;
return val;
},
isEmpty() {return index < 0}
}
return obj;
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment