Last active
March 21, 2017 21:39
-
-
Save egnedko/acd3a155cfdf1fde99e9247f503c534b to your computer and use it in GitHub Desktop.
Realisation Stack on js
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
class Stack extends Array { | |
constructor(STACKSIZE = Number.MAX_SAFE_INTEGER, ...arg) { | |
super(...arg); | |
Object.defineProperty(this, 'STACKSIZE', { | |
value: STACKSIZE | |
}) | |
this.push = this.push.bind(this); | |
this.pop = this.pop.bind(this); | |
} | |
push(value) { | |
if(this.length === this.STACKSIZE) { | |
let error = new Error("Stack overflow\n"); | |
error.name = 'Stack Error'; | |
return error.toString(); | |
} | |
return super.push(value); | |
} | |
pop() { | |
if(this.length === 0) { | |
let error = new Error("Stack underflow\n"); | |
error.name = 'Stack Error'; | |
return error.toString(); | |
} | |
return this.splice(-1,1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment