|
Hiya all, I'm just going to use a rough example here in pseudo code. Lets say that in Javascript I have an ObjectManager class:
Now lets say I have a class (randomClass):
The Question: Is the variable 'randomObject' a pointer to 'anObject', or is it a copy of 'anObject'?
(comments are locked)
|
|
Yes that is a simple implementation of singleton. As long as the instance of anObject is never replaced in ObjectManager it will continue to hand out that particular instance. Anything you do to the instance (any state changes) outside of the ObjectManager class will in fact be done to the private instance in the ObjectManager class. They are the same. There are no pointers in JS (the term of art is reference here), but I think that is what you mean. Also note that you can determine if two references point to the same instance on the heap with the == operator. refOne = ObjectManager.getAnObject(); refTwo = ObjectManager.getAnObject(); Then refOne == refTwo should be true. The story is a little different for strings in .NET but for this it's true.
Apr 22 '10 at 01:37 PM
raypendergraph
Thank-you so much ... that is exactly what I needed (and wanted) to know ;)
Apr 22 '10 at 03:15 PM
joeltebbett
(comments are locked)
|
|
It's a reference to anObject. You can have multiple references, all referencing a single object Thanks for the answer!
Apr 22 '10 at 03:16 PM
joeltebbett
(comments are locked)
|
|
Important: if you want it to be a singleton you'll need to declare the variable as static. Otherwise each instance of ObjectManager will have its own anObject, which is exactly the opposite of having a single one. (Also, ObjectManager won't compile unless the variable is static because it is being referred to in the static function getAnObject().
(comments are locked)
|

interested I am!