From c63fee437bc4b8fc9b3d5abac7f791159244051a Mon Sep 17 00:00:00 2001 From: Benjamin Petermann Date: Sat, 19 Aug 2023 14:27:45 +0200 Subject: [PATCH] Change singleton pattern counter class --- pages/patterns/design-patterns/singleton-pattern.mdx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pages/patterns/design-patterns/singleton-pattern.mdx b/pages/patterns/design-patterns/singleton-pattern.mdx index d177c8c..6ad35ee 100644 --- a/pages/patterns/design-patterns/singleton-pattern.mdx +++ b/pages/patterns/design-patterns/singleton-pattern.mdx @@ -51,9 +51,9 @@ let instance; class Counter { constructor() { if (instance) { - throw new Error("You can only create one instance!"); + return instance; } - this.counter = counter; + this.counter = 0; instance = this; } @@ -72,7 +72,7 @@ class Counter { // 2. Setting a variable equal to the the frozen newly instantiated object, by using the built-in `Object.freeze` method. // This ensures that the newly created instance is not modifiable. -const singletonCounter = Object.freeze(new Counter()); +const singletonCounter = new Counter(); // 3. Exporting the variable as the `default` value within the file to make it globally accessible. export default singletonCounter;