[ 3 posts ]

Akshar Prabhu Desai

YUI Contributor

  • Username: akshar.prabhudesai
  • Joined: Tue Apr 05, 2011 2:42 pm
  • Posts: 17
  • GitHub: akshar100
  • Gists: akshar100
  • Offline
  • Profile

How can I create singleton Modules through Y.Base ?

Post Posted: Wed Jun 20, 2012 8:16 am
+0-
I want some of my modules to be singleton. Generally in OOP this is achieved my making the constructor private. How do I do this in YUI3?

Juan Ignacio Dopazo

YUI Contributor

  • Username: jdopazo
  • Joined: Fri Oct 02, 2009 5:39 am
  • Posts: 618
  • Location: Buenos Aires, Argentina
  • Twitter: juandopazo
  • GitHub: juandopazo
  • Gists: juandopazo
  • Offline
  • Profile

Re: How can I create singleton Modules through Y.Base ?

Post Posted: Wed Jun 20, 2012 8:49 am
+0-
Singletons aren't necessary in JavaScript because you can create simple objects easily and those objects are mutable. In this case just create an instance of Y.Base, modify it and that's it.
Code:
var myObject = new Y.Base();
myObject.addAttrs({
  attrName: attrConfig
});
myObject.someMethod = function () {};

Nick Husher

YUI Contributor

  • Offline
  • Profile

Re: How can I create singleton Modules through Y.Base ?

Post Posted: Wed Jun 20, 2012 9:36 am
+0-
Alternatively:

Code:
var mySingleton = new (Y.Base.create('my-singleton', Y.Base, [], { /* object methods */ }, { /* static properties */ });


This will create a single instance of a Y.Base subclass and obfuscate the constructor of that instance.

I've gone down this road before, and generally I've found singletons to be less useful than they might be in other languages.
  [ 3 posts ]
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
cron