AreWe

Untitled

Jul 6th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. function Rat(name) {
  2. this.name = name;
  3. this.unitedRats = [];
  4.  
  5. Rat.prototype.unite = (rat) => {
  6. if (!(rat instanceof Rat)) {
  7. return;
  8. }
  9.  
  10. this.unitedRats.push(rat);
  11. };
  12.  
  13. Rat.prototype.getRats = () => this.unitedRats;
  14.  
  15. Rat.prototype.toString = () => `${this.name}${this.unitedRats.map((r) => `\n##${r.name}`).join('')}`;
  16. }
  17.  
  18. let firstRat = new Rat('Peter');
  19. console.log(firstRat.toString()); // Peter
  20.  
  21. console.log(firstRat.getRats()); // []
  22.  
  23. firstRat.unite(new Rat('George'));
  24. firstRat.unite(new Rat('Alex'));
  25. console.log(firstRat.getRats());
  26. // [ Rat { name: 'George', unitedRats: [] },
  27. // Rat { name: 'Alex', unitedRats: [] } ]
  28.  
  29. console.log(firstRat.toString());
  30. // Peter
  31. // ##George
  32. // ##Alex
Advertisement
Add Comment
Please, Sign In to add comment