最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Singleton class in coffeescript - Stack Overflow

programmeradmin1浏览0评论

I use singleton pattern class in coffeescript which shown below recently. It works perfectly but I don't know why this could be a singleton pattern. This might be a stupid question but thanks for your answering.

#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 code below is javascript which is produced 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 the definition of 'singleton pattern' nor how they are generally created but the reason why the code above always returns the same instance instead of creating new one.

I use singleton pattern class in coffeescript which shown below recently. It works perfectly but I don't know why this could be a singleton pattern. This might be a stupid question but thanks for your answering.

#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 code below is javascript which is produced 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 the definition of 'singleton pattern' nor how they are generally created but the reason why the code above always returns the same instance instead of creating new one.

Share Improve this question edited Sep 16, 2014 at 23:38 suish asked Sep 14, 2014 at 0:07 suishsuish 3,3631 gold badge19 silver badges36 bronze badges 1
  • You might look into running this code in a debugger, to see 'why' or 'how' this code does what it does. – Dean Radcliffe Commented Sep 17, 2014 at 2:36
Add a ment  | 

3 Answers 3

Reset to default 9

First of all, there is a good example of Singleton Pattern implementation in CoffeeScript 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 with CoffeeScript syntax a little bit. Here is how it should look:

class BaseClass
  class Singleton

  singleton = new Singleton()

  constructor: ->
    return singleton

Note that I'm using an explicit return here. I'm doing it because CoffeeScript implicit return doesn't work for class constructors.

I would also remend you to take a look at Simplest/Cleanest way to implement singleton in JavaScrip question.

My favorite singleton implementation is the following one:

class BaseClass
  instance = null

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

It works like yours except for two things:

  • it doesn't require an additional class definition;
  • it creates first singleton instance only when it's needed.

From Wikipedia,

The singleton pattern is a design pattern that restricts the instantiation of a class to one object

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

You may use this if there is only supposed to be a single instance of something on a page, such as an AJAX control. In JavaScript, you can't prevent someone from calling your function with "new", as in var b = new BaseClass(), but you can ensure that only a single instance is ever returned using this pattern.

You just have to declare an instance variable that is used only to contain your instance.

  Singleton = ->
    self           = this

    self.foo = ->
      # stuff here

    self

  module.exports = @instance ?= new Singleton()
发布评论

评论列表(0)

  1. 暂无评论