Advertisement
dimitrix85

03.Post

Nov 10th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solveClasses() {
  2.     class Post {
  3.         constructor(title, content) {
  4.             this.title = title;
  5.             this.content = content;
  6.         }
  7.  
  8.         toString() {
  9.             return `Post: ${this.title}\nContent: ${this.content}`;
  10.         }
  11.     }
  12.  
  13.     class SocialMediaPost extends Post {
  14.         constructor(title, content, likes, dislikes) {
  15.             super(title, content);
  16.             this.likes = likes;
  17.             this.dislikes = dislikes;
  18.             this.comments = [];
  19.         }
  20.  
  21.         addComment(comment) {
  22.             this.comments.push(comment);
  23.         }
  24.  
  25.         toString() {
  26.             return this.comments.length === 0 ?
  27.                 super.toString() + `\nRating: ${this.likes - this.dislikes}` :
  28.                 super.toString() + `\nRating: ${this.likes - this.dislikes}\nComments:\n` +
  29.                 this.comments.map(x => ` * ${x}`).join("\n");
  30.         }
  31.     }
  32.  
  33.     class BlogPost extends Post {
  34.         constructor(title, content, views) {
  35.             super(title, content);
  36.             this.views = views;
  37.         }
  38.  
  39.         view() {
  40.             this.views++;
  41.             return this;
  42.         }
  43.  
  44.         toString() {
  45.             return super.toString() + `\nViews: ${this.views}`;
  46.         }
  47.     }
  48.  
  49.     return {
  50.         Post,
  51.         SocialMediaPost,
  52.         BlogPost
  53.     }
  54. }
  55.  
  56. function solveProto(){
  57.  
  58.     function Post(title,content){
  59.         this.title = title;
  60.         this.content = content;
  61.     };
  62.  
  63.     Post.prototype.toString = function(){
  64.         return `Post: ${this.title}\nContent: ${this.content}`;
  65.     };
  66.  
  67.     function SocialMediaPost(title, content, likes, dislikes){
  68.         Post.call(this,title,content);
  69.         this.likes = likes;
  70.         this.dislikes = dislikes;
  71.         this.comments = [];
  72.     }
  73.  
  74.     SocialMediaPost.prototype.addComment = function(comment) {
  75.         this.comments.push(comment);
  76.     };
  77.  
  78.     SocialMediaPost.prototype.toString = function(){
  79.         return this.comments.length === 0 ?
  80.                 `Post: ${this.title}\nContent: ${this.content}\nRating: ${this.likes - this.dislikes}` :
  81.                 `Post: ${this.title}\nContent: ${this.content}\nRating: ${this.likes - this.dislikes}\nComments:\n` +
  82.                 this.comments.map(x => ` * ${x}`).join("\n");
  83.     }
  84.  
  85.     // SocialMediaPost.prototype = Object.create(Post.prototype);
  86.  
  87.     Object.setPrototypeOf(SocialMediaPost,Post);
  88.     Post.prototype.constructor = Post;
  89.  
  90.     function BlogPost(title, content, views){
  91.         Post.call(this,title,content);
  92.         this.views = views;
  93.     }
  94.  
  95.     BlogPost.prototype.view = function() {
  96.         this.views++;
  97.         return this;
  98.     };
  99.  
  100.     BlogPost.prototype.toString = function() {
  101.         return `Post: ${this.title}\nContent: ${this.content}\nViews: ${this.views}`;
  102.     }
  103.  
  104.     // BlogPost.prototype = Object.create(Post.prototype)
  105.     Object.setPrototypeOf(BlogPost,Post);
  106.     BlogPost.prototype.constructor = BlogPost;
  107.  
  108.     return {
  109.         Post,
  110.         SocialMediaPost,
  111.         BlogPost
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement