Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Rat(name) {
- this.name = name;
- this.unitedRats = [];
- Rat.prototype.unite = (rat) => {
- if (!(rat instanceof Rat)) {
- return;
- }
- this.unitedRats.push(rat);
- };
- Rat.prototype.getRats = () => this.unitedRats;
- Rat.prototype.toString = () => `${this.name}${this.unitedRats.map((r) => `\n##${r.name}`).join('')}`;
- }
- let firstRat = new Rat('Peter');
- console.log(firstRat.toString()); // Peter
- console.log(firstRat.getRats()); // []
- firstRat.unite(new Rat('George'));
- firstRat.unite(new Rat('Alex'));
- console.log(firstRat.getRats());
- // [ Rat { name: 'George', unitedRats: [] },
- // Rat { name: 'Alex', unitedRats: [] } ]
- console.log(firstRat.toString());
- // Peter
- // ##George
- // ##Alex
Advertisement
Add Comment
Please, Sign In to add comment