kema

3.Posts

Nov 3rd, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Post {
  2.         constructor(title, content) {
  3.             this.title = title;
  4.             this.content = content;
  5.         }
  6.  
  7.         toString() {
  8.             return `Post: ${this.title}
  9. Content: ${this.content}`;
  10.         }
  11.     }
  12.     class SocialMediaPost extends Post {
  13.         constructor(title, content, likes, dislikes) {
  14.             super(title, content);
  15.             this.likes = likes;
  16.             this.dislikes = dislikes;
  17.             this.comments = [];
  18.         }
  19.  
  20.         addComment(comment) {
  21.             this.comments.push(comment);
  22.         }
  23.  
  24.         toString() {
  25.             let baseStr = super.toString();
  26.             if (this.comments.length > 0) {
  27.                 return baseStr + `\nRating: ${Number(this.likes) - Number(this.dislikes)}`
  28.                     + `\nComments: \n* ${this.comments.join('\n* ')}`;
  29.  
  30.             }
  31.             return baseStr + `\nRating: ${this.likes} - ${this.dislikes}`;
  32.         }
  33.     }
  34.     class BlogPost extends Post {
  35.         constructor(title, content) {
  36.             super(title, content);
  37.             this.views = this.view();
  38.         }
  39.  
  40.         view() {
  41.             if (!this.views) {
  42.                 this.views = 0;
  43.             }
  44.             return this.views++;
  45.  
  46.         }
  47.  
  48.         toString() {
  49.             let baseStr = super.toString();
  50.             return baseStr + `\nViews: ${this.views}`
  51.         }
  52.     }
  53.     let scm = new BlogPost("TestTitle", "TestContent");
  54.     console.log(scm.toString());
  55.     scm.view();
  56.     console.log(scm.toString());
Advertisement
Add Comment
Please, Sign In to add comment