Skip to content

Instantly share code, notes, and snippets.

@Alexandre-cibot
Created September 26, 2019 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alexandre-cibot/cf8ab990ab671b5a8a8ccd26e9744854 to your computer and use it in GitHub Desktop.
Save Alexandre-cibot/cf8ab990ab671b5a8a8ccd26e9744854 to your computer and use it in GitHub Desktop.
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