Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pages/patterns/design-patterns/factory-pattern.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ However, in many cases it may be more memory efficient to create new instances i

```js
class User {
constructor(firstName, lastName, email) {
constructor({ firstName, lastName, email }) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
Expand All @@ -72,6 +72,10 @@ class User {
const posts = await fetch(`https://my.cms.com/posts/user/${this.id}`);
return posts;
}

fullName() {
return `${this.firstName} ${this.lastName}`;
}
}

const user1 = new User({
Expand All @@ -85,6 +89,9 @@ const user2 = new User({
lastName: "Doe",
email: "jane@doe.com",
});

console.log(user1.fullName()); // John Doe
console.log(user2.fullName()); // Jane Doe
```

The `fullName` method is the same for all the objects that were created. By creating new instances, the `fullName` method is available on the prototype instead of on the objec, which saves memory.
Expand Down