How to create a classmate subclass of coffeescript

I created a singleton class that I want to extend. It (half) works in that it creates only one instance of the class, but the properties added to the subclass are undefined. Here is the original singleton:

class Singleton _instance = undefined @getInstance: -> if _instance is undefined console.log 'no instance exists, so create one' _instance = new _Singleton() else console.log 'an instance already exists.' class _Singleton constructor: -> console.log 'new singelton' module.exports = Singleton 

And here is the subclass:

 Singleton = require('./singleton') class Stinky extends Singleton constructor: -> var1 : 'var1' module.exports = Stinky 

Now, if I use the following in my node application:

 Stinky = require './stinky' thing1 = Stinky.getInstance() thing2 = Stinky.getInstance() console.log "Thing var1: #{thing1.var1}" 

The getInstance () method behaves as expected, but var1 is undefined. If I do the same on non-singleton classes, they work fine. Thanks.

+6
source share
3 answers

I cut your code a bit. Here are the two remaining classes:

 class Singleton @_instance: null @getInstance: -> @_instance or= new @( arguments... ) class Stinky extends Singleton constructor: ( @num ) -> thing1 = Stinky.getInstance( 1 ) thing2 = Stinky.getInstance( 2 ) console.log( thing1.num, thing2.num ) 

I made the following changes:

  • United Singleton and _Singleton
  • Changed _instance for @_instance so that it is bound to Singleton and not to its prototype
  • Added splat arguments to getInstance (if necessary)
  • Pointing getInstance () to an extended object, not a Singleton

In this example, I used 2 different numbers to ensure that the second constructor was never called.

+12
source

I see how you use the _Singleton class to try to mimic a private class, but unfortunately I do not think you can use it in this case.

Here is the code that works:

 class Singleton _instance = undefined constructor: -> console.log 'new singleton' @getInstance: -> if _instance is undefined console.log 'no instance exists, so create one' _instance = new @() else console.log 'an instance already exists.' _instance class Stinky extends Singleton constructor: -> console.log 'Stinky constructor' @var1 = 'var1' thing1 = Stinky.getInstance() thing2 = Stinky.getInstance() console.log "Thing var1: #{thing1.var1}"​​​​​​​​​​​​​​​​​​, thing1, thing2​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ 

I removed the Node.js (require) code, but adding that it should be simple. The main difference is that the instance created by my code is an @ or this instance. This will ensure that your constructor is called first, and then continue the parent chain. Your code explicitly created an instance of _Singleton , so your Stinky constructor has never been called. Another minor issue that you eventually noticed was that your getInstance method did not actually return an _instance instance.

I hope this helps,

Sandro

+2
source

I'm not sure what the goal is, but you can achieve the same result by making Singleton real single (simple object):

 Singleton = doNothing: -> # ... doMoreNothing: -> # ... class Stinky constructor: -> @var1: 'var1' getInstance: -> return Singleton 

For Singleton it makes no sense to have a method that returns itself.

+1
source

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


All Articles