Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. class ArrayNb extends Array {
  2. constructor(props) {
  3. !!props ? super(props) : super();
  4. }
  5. push(...elements) {
  6. if ( [...elements].every(e => typeof e === "number") ){
  7. return super.push(...elements);
  8. } else {
  9. throw new Error("ArrayNb can only store numbers");
  10. }
  11. }
  12. populate(array) {
  13. if (Array.isArray(array)) {
  14. array.forEach(e => {
  15. this.push(e);
  16. })
  17. } else {
  18. throw new Error("ArrayNb.prototype.populate require an Array as argument");
  19. }
  20. }
  21. }
  22.  
  23. const a = new Array();
  24. const anb = new ArrayNb();
  25.  
  26. console.log(a);
  27. console.log(anb);
  28. console.log(a.push(3, 4));
  29. console.log(anb.push(3, 4));
  30.  
  31. console.log(a)
  32. console.log(anb)
  33.  
  34. console.log(a.join(''))
  35. console.log(anb.join(''))
  36.  
  37. anb.populate([671,2,0]);
  38. console.log(anb)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement