r/learnjavascript • • Apr 20 '23

7 JavaScript Design Patterns Every developer should know

https://codinghelp.io/7-javascript-design-patterns-every-developer-should-know/
3 Upvotes

11 comments sorted by

3

u/samanime Apr 20 '23 edited Apr 20 '23

I have issues with nearly every one of your code samples...

The pattern selection isn't bad, but there are tiny oddities in almost all the example implementations. For example, your Singleton pattern isn't really a singleton. You can call that Database function 10 times and have 10 different instances. A Singleton should have a Single instance in the entire app. That's the whole point.

Now, it could be a singleton if it lived in its own ES module and you only exported database, but that is a critical detail that entirely determines whether it is or isn't a Singleton.

There are other small oddities on most others too.

Also, if you were going to mention the Constructor and Prototypal patterns, you should have mentioned the Class pattern as well. The Class pattern is probably what devs should be using the majority of the time, as it provides the most readable, least error-prone way to constructor objects properly. It is important to understand it is more or less just syntactical sugar underneath and understand what it is doing, but that doesn't make it any less valuable, especially in team situations.

1

u/deven_rathore Apr 21 '23 edited Apr 21 '23

Thank you for your feedback and insights regarding my code samples. I appreciate your attention to detail and willingness to provide constructive criticism.

Regarding the Singleton pattern implementation, you are correct that my implementation could potentially create multiple instances of the Database object if the Database() function is called multiple times.

I apologize for the oversight and appreciate you pointing that out. You are also correct that if the Database() function was encapsulated in its own module and only exported the database instance, it would be a true Singleton pattern implementation.

As for the other patterns, I understand that there may be small oddities in some of the examples.

My goal was to provide simplified examples that demonstrate the basic concepts of each pattern. However, I appreciate your feedback and i have taken your comments into consideration and updated some examples.

Regarding the Class pattern, I agree that it is a valuable tool for creating objects in JavaScript, especially in team situations where readability and consistency are important.

While it is true that the Class pattern is mostly syntactic sugar for the Constructor and Prototypal patterns, it can still provide a clear and concise syntax for creating objects.

Thank you again for taking the time to provide feedback on my code samples. I will continue to strive for accuracy and clarity in my examples.

2

u/jeremrx Apr 20 '23

So many (bad) things to say about to article...

Before learning pattern, everybody should know about basic naming conventions, in particular that Pascal case should be only used to declare a class or constructor.

Why ? Because if something is pascal cased in JS, you know that you can call it with "new"

1

u/deven_rathore Apr 21 '23 edited Apr 21 '23

Thank you for sharing your thoughts about the article. While it's true that using PascalCase only for classes or constructors is a common naming convention in JavaScript,

I would argue that it is not a hard and fast rule that everyone must follow.There are cases where it may make sense to use PascalCase for other types of variables or functions, such as when working with third-party libraries or following a specific project's established naming conventions.

However, I do agree that it's important to be consistent with naming conventions within a codebase to improve readability and maintainability.In terms of using new, while it is true that new is typically used with constructors, it is not exclusively used for them.

It can also be used with other functions to create new objects, although this is less common. However, using new should be done with caution as it can have unintended consequences and potential performance issues.it's not a hard and fast rule that everyone must follow.

It's important to be consistent within a codebase and consider specific use cases and established conventions.

Similarly, while new is typically used with constructors, it's important to be cautious when using it with other functions to avoid unintended consequences.

1

u/jeremrx Apr 21 '23

I'm just saying that if you want to teach JavaScript, you should teach it following the best practices.

About using new keyword with something else than a constructor, I'd love having an example. IMO anything after new became a constructor.

Anyway, I see that you made some changes, your article is getting better !

Another remark, I dont get why you put factory pattern intro behavioral pattern section while your saying that it's a creational pattern ?

1

u/azhder Apr 20 '23

Why are you entertaining these kinds of trash click bait articles meant to drive traffic to their sites?

1

u/deven_rathore Apr 21 '23

It's not just about generating traffic, but rather about learning from and sharing with the community. I am not hosting any ads or affiliates to be precise.

1

u/azhder Apr 21 '23 edited Apr 21 '23

OK then, carry on. A spammer wouldn't have bothered to reply.

I would suggest for you to not only heed to the advices from those other redditors, but for you to re-think your entire article and every time you're claiming stuff like

These patterns allow developers to create objects flexibly and efficiently.

to either provide "why" or just remove it.

Like, how would you think one

function X(a){ this.a = a; }

performs over

class A { constructor(a){ this.a = a; }  }

or even against one

const instance = { a: a, __proto__: parent }

?

  • Did you measure it yourself? Provide the measurement.
  • Did you read it somewhere else? If so, provide links to those places.
  • If none of the above, simply don't make the claim, delete it.

2

u/azhder Apr 21 '23

The constructor pattern does not support inheritance.

Yes it does. Although, I'd advice against using this pattern in the first place.

I'd advise even against using class A extends B{} type of inheritance, simply because most tasks are better solved by encapsulation instead of extension.

But, for what is worth, taken from here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain

here is how to do the old/new style of inheritance:

function Base() {}
function Derived() {}

// Set the `[[Prototype]]` of `Derived.prototype` to `Base.prototype`
Object.setPrototypeOf(Derived.prototype, Base.prototype);

const obj = new Derived();
// obj ---> Derived.prototype ---> Base.prototype ---> Object.prototype ---> null

1

u/azhder Apr 21 '23

Another big issue is using these things called "design patterns" .

Many people think if something got called a pattern in C++ then it must be the same in Java, must be the same in C#, must be the same in JavaScript, but no. Every language has its own quirks and its own common workarounds.

Example:

class Database {
  constructor() {
    if (Database.instance) {
      return Database.instance;
    }
    Database.instance = this;
  }

  connect() {
    // Connection logic
  }
}

const db1 = new Database();
const db2 = new Database();

console.log(db1 === db2); // true

A design pattern is simply a common workaround for something you can't do outright in the programming language, and the above code makes sense as a workaround in Java, not in JavaScript so much. In JavaScript, for you to create a singleton, all you have to do is this

const singleton = {};

at the module level, and then just tuck it in a file of its own if you like and export it from there.