Пользователь, Модератор и Администратор с ООП Javascript - PullRequest
0 голосов
/ 02 октября 2018

Задача

Для этого задания вы создадите секцию комментариев.Дизайн

Мы собираемся сосредоточиться на двух аспектах: Пользователи

Users come in 3 flavors, normal users, moderators, and admins. Normal users can only create new comments, and edit the their own comments. Moderators have the added ability to delete comments (to remove trolls), while admins have the ability to edit or delete any comment.
Users can log in and out, and we track when they last logged in

Комментарии

Comments are simply a message, a timestamp, and the author.
Comments can also be a reply, so we'll store what the parent comment was.

Ниже мой код:

class Admin extends Moderator {
  constructor(name) {
    super(name);
  }
  canEdit(comment) {
    return true;
  }
}

class Comment {
  constructor(author, message, repliedTo) {
    this.createdAt = new Date();
    this._author = author;
    this._message = message;
    this.repliedTo = repliedTo || null;
  }
  getMessage() {
    return this._message;
  }
  setMessage(message) {
    this._message = message;
  }
  getCreatedAt() {
    return this.createdAt;
  }
  getAuthor() {
    return this._author;
  }
  getRepliedTo() {
    return this.repliedTo;
  }
  getString(comment) {
    const authorName = comment.getAuthor().getName();
    if (!comment.getRepliedTo()) return authorName;
    return `${comment.getMessage()} by ${authorName} (replied to ${this.getString(comment.getRepliedTo())})`;
  }
  toString() {
    const authorName = this.getAuthor().getName();
    if (!this.getRepliedTo()) {
      return `${this._message} by ${authorName}`;
    }
    return this.getString(this);
  }
}

Я получаю сообщение об ошибке «Метод toString должен возвращать правильную иерархию (вложенный ответ)», может кто-нибудь помочь с этим

Ответы [ 5 ]

0 голосов
/ 12 мая 2019
  constructor(name) {
   this._name = name;
   this._loggedIn = false;
   this._lastLoggedInAt = null;
  }
  isLoggedIn() {
    return this._loggedIn;
  }
  getLastLoggedInAt() {
    return this._lastLoggedInAt;
  }
  logIn() {
    this._lastLoggedInAt = new Date();
    this._loggedIn = true;
  }
  logOut() {
    this._loggedIn = false
  }
  getName() {
    return this._name;
  }
  setName(name) {
    this._name = name;
  }
  canEdit(comment) {
    if(comment._author._name === this._name) {
      return true;
    }
    return false;
  }
  canDelete(comment) {
    return false;
  }
}

class Moderator extends User {
   constructor(name) {
     super(name);
   }
   canDelete(comment) {
     return true;
   }
}

class Admin extends Moderator {
  constructor(name) {
    super(name)
  }
  canEdit(comment) {
    return true;
  }
}

class Comment {
   constructor(author = null, message, repliedTo = null) {
     this._createdAt = new Date();
     this._message = message;
     this._repliedTo = repliedTo;
     this._author = author;
   }
   getMessage() {
     return this._message;
   }
   setMessage(message) {
     this._message = message;
   }
   getCreatedAt() {
     return this._createdAt;
   }
   getAuthor() {
     return this._author;
   }
   getRepliedTo() {
     return this._repliedTo;
   }
   toString() {
     if(this._repliedTo === null) {
        return this._message + " by " + this._author._name
     }
     return this._message + " by " + this._author._name + " (replied to " + 
          this._repliedTo._author._name + ")"
   }
 }
0 голосов
/ 12 ноября 2018
class User {
  function __construct($name) {
   private $name;
   private $loggedIn;
   private $lastLoggedInAt;

   $this->name = $name;
   $this->loggedIn = false;
   $this->lastLoggedInAt = null;
  }
  function isLoggedIn() {
    return $this->loggedIn;
  }
  function getLastLoggedInAt() {
    return $this->lastLoggedInAt;
  }
  function logIn() {
    $this->lastLoggedInAt = new Date('Y-m-d H:i:s');
    $this->loggedIn = true;
  }
  function logOut() {
    $this->loggedIn = false;
  }
  function getName() {
    return $this->name;
  }
  function setName($name) {
    $this->name = $name;
  }
  function canEdit($comment) {
    if($comment->author->name === $this->name) {
      return true;
    }
    return false;
  }
  function canDelete($comment) {
    return false;
  }
}

class Moderator extends User {
   function __construct($name) {
     $this->name = $name;
   }
   function canDelete($comment) {
     return true;
   }
}

class Admin extends Moderator {
  function constructor($name) {
    $this->name = $name;
  }
  function canEdit($comment) {
    return true;
  }
}

class Comment {
   function __construct($author = null, $message, $repliedTo = null) {

    private $createdAt;
    private $message;
    private $repliedTo;
    private $author;

     $this->createdAt = new Date('Y-m-d H:i:s');
     $this->message = $message;
     $this->repliedTo = $repliedTo;
     $this->author = $author;
   }
   function getMessage() {
     return $this->message;
   }
   function setMessage($message) {
     $this->message = $message;
   }
   function getCreatedAt() {
     return $this->createdAt;
   }
   function getAuthor() {
     return $this->author;
   }
   function getRepliedTo() {
     return $this->repliedTo;
   }
   function __toString() {
     if($this->repliedTo === null) {
        return $this->message + " by " + $this->author->name;
     }
     return $this->message + " by " + $this->author->name + " (replied to " + 
          $this->repliedTo->author->name + ")";
   }
 }
0 голосов
/ 23 октября 2018

Вы можете удалить метод getString () ...

toString() 
{
    return ((this._repliedTo === null) ? this._message + " by " + 
           this._author.getName() : this._message + " by " + 
           this._author.getName() + " (replied to " + this._repliedTo._author.getName() + ")");
}
0 голосов
/ 31 октября 2018

Хотя это должно быть задание, вопрос был немного техническим и неясным;Это проверенное решение

class User {
  constructor(name) {
   this._name = name;
   this._loggedIn = false;
   this._lastLoggedInAt = null;
  }
  isLoggedIn() {
    return this._loggedIn;
  }
  getLastLoggedInAt() {
    return this._lastLoggedInAt;
  }
  logIn() {
    this._lastLoggedInAt = new Date();
    this._loggedIn = true;
  }
  logOut() {
    this._loggedIn = false
  }
  getName() {
    return this._name;
  }
  setName(name) {
    this._name = name;
  }
  canEdit(comment) {
    if(comment._author._name === this._name) {
      return true;
    }
    return false;
  }
  canDelete(comment) {
    return false;
  }
}

class Moderator extends User {
   constructor(name) {
     super(name);
   }
   canDelete(comment) {
     return true;
   }
}

class Admin extends Moderator {
  constructor(name) {
    super(name)
  }
  canEdit(comment) {
    return true;
  }
}

class Comment {
   constructor(author = null, message, repliedTo = null) {
     this._createdAt = new Date();
     this._message = message;
     this._repliedTo = repliedTo;
     this._author = author;
   }
   getMessage() {
     return this._message;
   }
   setMessage(message) {
     this._message = message;
   }
   getCreatedAt() {
     return this._createdAt;
   }
   getAuthor() {
     return this._author;
   }
   getRepliedTo() {
     return this._repliedTo;
   }
   toString() {
     if(this._repliedTo === null) {
        return this._message + " by " + this._author._name
     }
     return this._message + " by " + this._author._name + " (replied to " + 
          this._repliedTo._author._name + ")"
   }
 }

Ошибка была в том, что вы вызывали метод getName() для метода getAuthor, который не был доступен.Вы можете получить имя автора прямо из комментария this._author._name.

0 голосов
/ 03 октября 2018

Я использую стиль кодирования конструктора JavaScript, чтобы написать это решение, но это не должно иметь большого значения, так как решение не требует, чтобы вы изменили свой стиль.Обратите внимание, что поля (_author, _message, _repliedTo) являются закрытыми, а к закрытым полям можно получить доступ только через открытые методы.И это в основном то, что я сделал здесь в методе toString ().

function Comment(author, message, repliedTo = null) {
  var _author = author;
  var _message = message;
  var _repliedTo = repliedTo;
  
  
  this.getAuthor = function() {
    return _author;
  };
  
  this.getRepliedTo = function() {
    return _repliedTo;
  };
  
  this.toString = function() {
    return ((_repliedTo === null) ? message + " by " + _author.getName() : message + " by " + _author.getName() + " (replied to " + this.getRepliedTo().getAuthor().getName() + ")");
  }
};
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...