Skip to content

Instantly share code, notes, and snippets.

@Mordorreal
Created January 7, 2017 20:34
Show Gist options
  • Save Mordorreal/df25cb3358ab27e83c3f6ff382c2ec0f to your computer and use it in GitHub Desktop.
Save Mordorreal/df25cb3358ab27e83c3f6ff382c2ec0f to your computer and use it in GitHub Desktop.
JS mimics array object
class List {
constructor(...args) {
this.store = [...args];
this.length = this.store.length;
this.createIndex();
}
createIndex() {
for (let i in this.store) {
this[i] = this.store[i];
}
}
push = (...el) => {
this.store.push(...el);
this.length = this.store.length;
this.createIndex();
return this.length;
}
pop = () => {
const el = this.store.pop();
this.length = this.store.length;
this[this.length] = undefined;
return el;
}
shift = () => {
const el = this.store.shift();
this.length = this.store.length;
this[this.length] = undefined;
this.createIndex();
return el;
}
unshift = (...el) => {
this.store.unshift(...el);
this.length = this.store.length;
this.createIndex();
return this.length;
}
filter = (func) => {
return this.store.filter(func);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment