Skip to content

Instantly share code, notes, and snippets.

@w3cj
Created August 1, 2018 13:39
Show Gist options
  • Save w3cj/c103c0393440e200598060e61d7769f7 to your computer and use it in GitHub Desktop.
Save w3cj/c103c0393440e200598060e61d7769f7 to your computer and use it in GitHub Desktop.
class Array {
constructor() {
this.length = 0;
}
push(value) {
this[this.length] = value;
this.length++;
}
pop() {
const value = this[this.length - 1];
this.length--;
return value;
}
forEach(fn) {
// iterate over the array
// invoke fn with each value in the array
}
}
const arr = new Array();
arr.push(1);
console.log(arr.length);
arr.push(10);
console.log(arr.length);
console.log(arr[0]); // -> 1
console.log(arr[1]); // -> 10
console.log(arr.pop()); // -> 10
console.log(arr.length); // -> 1
arr.forEach((value, index, array) => {
console.log(value, index, array);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment