115 words
1 minutes
Maker Pattern in JavaScript
The maker pattern in JavaScript works by creating a function that, in turn, creates objects. This pattern is also known as the factory function pattern. Here’s a simple example:
const createUser = (firstName, lastName, city) => {
return {
firstName: firstName,
lastName: lastName,
city: city,
};
};
This is very useful and often better than constructor functions for several reasons:
Avoids Boilerplate Code
: Constructor functions require the use of thethis
keyword, which can be verbose and sometimes confusing for new developers.Simplicity
: The implementation is clean and easy to follow. You can quickly see the structure of the object being created.No new Keyword
: Unlike constructor functions, you don’t need to use the new keyword to create an instance.
Maker Pattern in JavaScript
https://rabisiddique.com/posts/maker-pattern/Author
Rabi Siddique
Published at
2024-06-02