Singleton class in coffeescript

I am using a singleton pattern class in coffeescript, which is shown below recently. It works fine, but I don't know why this might be a singleton pattern. This might be a dumb question, but thanks for your answer.

#coffeescript class BaseClass class Singleton singleton = new Singleton() BaseClass = -> singleton a = new BaseClass() a.name = "John" console.log a.name # "John" b = new BaseClass() b.name = "Lisa" console.log b.name # "Lisa" console.log a.name # "Lisa" 

and the code below is javascript which is created by the code above

 var BaseClass, a, b; BaseClass = (function() { var Singleton, singleton; function BaseClass() {} Singleton = (function() { function Singleton() {} return Singleton; })(); singleton = new Singleton(); BaseClass = function() { return singleton; }; return BaseClass; })(); a = new BaseClass(); a.name = "John"; console.log(a.name); b = new BaseClass(); b.name = "Lisa"; console.log(b.name); console.log(a.name); 

EDITED: I am not asking for the definition of "singleton pattern" and how they are created at all, but the reason why the code above always returns the same instance instead of creating a new one.

+6
source share
4 answers

First of all, there is a good example of implementing Singleton Pattern in a Cookbook Cookbook :

 class Singleton instance = null class PrivateClass constructor: (@message) -> echo: -> @message @get: (message) -> instance ?= new PrivateClass(message) 

You tried to do a similar thing, but messed up a bit with CoffeeScript syntax. Here's how it should look:

 class BaseClass class Singleton singleton = new Singleton() constructor: -> return singleton 

Note that I am using explicit return here. I do this because the CoffeeScript implicit return does not work for class constructors.

I also recommend that you take a look at the easiest / cleanest way to implement singleton in JavaScrip .

My favorite singleton implementation is as follows:

 class BaseClass instance = null constructor: -> if instance return instance else instance = this # contructor code 

It works like yours, except for two things:

  • no additional class definition is required;
  • it creates the first singleton instance only when necessary.
+9
source

From Wikipedia ,

The syntax pattern is a design pattern that restricts the instantiation of a class to a single object

Since in your example the change in a reflected in b , it shows that they are the same object, so this is a Singleton pattern.

You can use this if it is only assumed that there is only one instance on the page, such as an AJAX control. In JavaScript, you cannot prevent someone from var b = new BaseClass() your function with "new", as in var b = new BaseClass() , but you can guarantee that only one instance is ever returned using this template.

0
source

Because new BaseClass() same as BaseClass() when BaseClass() returns an object.
In this case, BaseClass() returns a singleton object, so new BaseClass() also returns singleton .
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new

-1
source

You just need to declare an instance variable, which is used only to host your instance.

  Singleton = -> self = this self.foo = -> # stuff here self module.exports = @instance ?= new Singleton() 
-1
source

Source: https://habr.com/ru/post/975245/


All Articles