Created
May 8, 2020 13:32
-
-
Save remixer-dec/33fa16c0f4caa39eb460c4ee113ce3c6 to your computer and use it in GitHub Desktop.
Fixed size / length array (LiFo) for infinite pushing
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
//Warning: fixed size is only kept for .push() method | |
//Usage: | |
//let arr = new FixedSizeArrayLifo(2) | |
//arr.push(123); ... | |
class FixedSizeArrayLiFo extends Array { | |
constructor(size) { | |
super() | |
this.#size = size | |
} | |
#size | |
push(value) { | |
super.push(value) | |
if (this.length > this.#size) { | |
this.shift() | |
} | |
return this.length | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment