| Page 1 of 1 | [ 3 posts ] |
|
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 DopazoYUI Contributor
|
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 () {}; |
|
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. |
| Page 1 of 1 | [ 3 posts ] |
| 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 |
© 2006-2013 Yahoo! Inc. All rights reserved.
All code on this site is licensed under the BSD License unless stated otherwise.
About This Site · Security Contact Info
Powered by phpBB® Forum Software © phpBB Group