Social Media
unknown
plain_text
2 years ago
3.1 kB
3
Indexable
function main() { class User { #name; #email; #password; #posts; constructor(name, email, password) { this.#name = name; this.#email = email; this.#password = password; this.#posts = []; } get name() { return this.#name; } set name(newName) { this.#name = newName; } get email() { return this.#email; } set email(newEmail) { this.#email = newEmail; } get password() { return this.#password; } set password(newPassword) { this.#password = newPassword; } addPost(post) { this.#posts.push(post); } deletePost(post) { this.#posts = this.#posts.filter(p => p.title !== post.title); } displayPosts() { console.log(`Posts by ${this.#name}:`); this.#posts.forEach(post => { console.log(`- ${post.title}`); }); } } class Post extends User { #title; #content; #date; #likeCount; constructor(name, email, password, title, content, date) { super(name, email, password); this.#title = title; this.#content = content; this.#date = date; this.#likeCount = 0; } get title() { return this.#title; } set title(newTitle) { this.#title = newTitle; } get content() { return this.#content; } set content(newContent) { this.#content = newContent; } get date() { return this.#date; } set date(newDate) { this.#date = newDate; } addLike() { this.#likeCount += 1; } displayDetails() { console.log(`Owner: ${this.name}`); console.log(`Title: ${this.#title}`); console.log(`Content: ${this.#content}`); console.log(`Date: ${this.#date}`); console.log(`Likes: ${this.#likeCount}`); } } const user1 = new User("John", "john@example.com", "password123"); const post1 = new Post( "John", "john@example.com", "password123", "My first post", "Lorem ipsum dolor sit amet", "2021-01-01" ); const post2 = new Post( "John", "john@example.com", "password123", "My second post", "Consectetur adipiscing elit", "2021-01-02" ); user1.addPost(post1); user1.addPost(post2); post1.addLike(); post1.addLike(); user1.displayPosts(); // Output: // Posts by John: // - My first post // - My second post post1.displayDetails(); // Output: // Owner: John // Title: My first post // Content: Lorem ipsum dolor sit amet // Date: 2021-01-01 // Likes: 2 return { User, Post }; } main();
Editor is loading...
Leave a Comment