Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solveClasses() {
- class Pet {
- constructor(owner, name) {
- this.owner = owner;
- this.name = name;
- this.comments = [];
- }
- addComment(comment) {
- this.comments.forEach(c => {
- if (c === comment) {
- throw new Error('This comment is already added!');
- }
- });
- this.comments.push(comment);
- return 'Comment is added.';
- }
- feed() {
- return `${this.name} is fed`;
- }
- toString() {
- let result = `Here is ${this.owner}'s pet ${this.name}.`
- if (this.comments.length > 0) {
- result += `\nSpecial requirements: ${this.comments.join(', ')}`;
- }
- return result;
- }
- }
- class Cat extends Pet {
- constructor( owner, name, insideHabits, scratching ) {
- super(owner, name);
- this.insideHabits = insideHabits;
- this.scratching = scratching;
- }
- feed() {
- return super.feed() + ', happy and purring.';
- }
- toString() {
- let result = super.toString() + '\n' + `Main information:\n${this.name} is a cat with ${this.insideHabits}`;
- if (this.scratching) {
- result += ', but beware of scratches.';
- }
- return result;
- }
- }
- class Dog extends Pet {
- constructor(owner, name, runningNeeds, trainability) {
- super(owner, name);
- this.runningNeeds = runningNeeds;
- this.trainability = trainability;
- }
- feed() {
- return super.feed() + ', happy and wagging tail.';
- }
- toString() {
- let result = super.toString() + '\n' + 'Main information:\n';
- result += `${this.name} is a dog with need of ${this.runningNeeds}km running every day and ${this.trainability} trainability.`;
- return result;
- }
- }
- return {
- Pet,
- Cat,
- Dog
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement