Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Post {
- constructor(title, content) {
- this.title = title;
- this.content = content;
- }
- toString() {
- return `Post: ${this.title}
- Content: ${this.content}`;
- }
- }
- class SocialMediaPost extends Post {
- constructor(title, content, likes, dislikes) {
- super(title, content);
- this.likes = likes;
- this.dislikes = dislikes;
- this.comments = [];
- }
- addComment(comment) {
- this.comments.push(comment);
- }
- toString() {
- let baseStr = super.toString();
- if (this.comments.length > 0) {
- return baseStr + `\nRating: ${Number(this.likes) - Number(this.dislikes)}`
- + `\nComments: \n* ${this.comments.join('\n* ')}`;
- }
- return baseStr + `\nRating: ${this.likes} - ${this.dislikes}`;
- }
- }
- class BlogPost extends Post {
- constructor(title, content) {
- super(title, content);
- this.views = this.view();
- }
- view() {
- if (!this.views) {
- this.views = 0;
- }
- return this.views++;
- }
- toString() {
- let baseStr = super.toString();
- return baseStr + `\nViews: ${this.views}`
- }
- }
- let scm = new BlogPost("TestTitle", "TestContent");
- console.log(scm.toString());
- scm.view();
- console.log(scm.toString());
Advertisement
Add Comment
Please, Sign In to add comment