Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // @ts-check
- class Title {
- #title;
- /**
- * @param {string} title
- */
- constructor(title) {
- if (typeof title != 'string') {
- throw new Error('input argument must be a string');
- }
- if (title.length > 80) {
- throw new Error('Title is too long');
- }
- this.#title = title;
- }
- get title() {
- return this.#title;
- }
- }
- class Content {
- #content;
- /** @param {string} content */
- constructor(content) {
- if (typeof content != 'string') {
- throw new Error('input argument must be a string');
- }
- this.#content = content;
- }
- get content() {
- return this.#content;
- }
- }
- class Url {
- #url;
- /** @param {string} url */
- constructor(url) {
- if (typeof url != 'string') {
- throw new Error('input argument must be a string');
- }
- if (!url.startsWith('http://') && !url.startsWith('https://')) {
- throw new Error('Invalid URL');
- }
- this.#url = url;
- }
- get url() {
- return this.#url;
- }
- }
- class Post {
- /**
- * @param {Title} title
- * @param {Content} content
- * @param {Url} imageUrl
- */
- constructor(title, content, imageUrl) {
- if (!(title instanceof Title)) {
- throw new Error('title must be an instance of Title');
- }
- if (!(content instanceof Content)) {
- throw new Error('content must be an instance of Content');
- }
- if (!(imageUrl instanceof Url)) {
- throw new Error('imageUrl must be an instance of Url');
- }
- this._title = title;
- this._content = content;
- this._imageUrl = imageUrl;
- }
- /** @returns {string} */
- get title() {
- return this._title.title;
- }
- /** @returns {string} */
- get content() {
- return this._content.content;
- }
- /** @returns {string} */
- get imageUrl() {
- return this._imageUrl.url;
- }
- toString() {
- return `${this.title}\n${this.content}\n${this.imageUrl}`;
- }
- }
- const post = new Post(
- new Title('This is a title.'),
- new Content('This is a post.'),
- new Url('http://example.com'));
- console.log(post.toString());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement