Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class ArrayNb extends Array {
constructor(props) {
!!props ? super(props) : super();
}
push(...elements) {
if ( [...elements].every(e => typeof e === "number") ){
return super.push(...elements);
} else {
throw new Error("ArrayNb can only store numbers");
}
}
populate(array) {
if (Array.isArray(array)) {
array.forEach(e => {
this.push(e);
})
} else {
throw new Error("ArrayNb.prototype.populate require an Array as argument");
}
}
}
const a = new Array();
const anb = new ArrayNb();
console.log(a);
console.log(anb);
console.log(a.push(3, 4));
console.log(anb.push(3, 4));
console.log(a)
console.log(anb)
console.log(a.join(''))
console.log(anb.join(''))
anb.populate([671,2,0]);
console.log(anb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment