Created
September 26, 2019 15:45
-
-
Save Alexandre-cibot/cf8ab990ab671b5a8a8ccd26e9744854 to your computer and use it in GitHub Desktop.
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
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