![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi guys, how do you make a singleton access class? Do you know a better way of this one: var singletonClass = (function( ) { // Private variable var instance = null; // Private Constructor function myClass( ) { //... } return new function( ) { this.constructor = null; this.getInstance = function( ) { if( ! instance ) { instance = new myClass( ); instance.constructor = null; } return instance; } } })( ); Pure qui? :-)) |
#3
| |||
| |||
|
|
Do you know a better way of this one: Yes |
#4
| |||
| |||
|
|
Do you know a better way of this one: Yes change this: ** *return new function( ) to this: * * return function( ) ;-) |
#5
| |||
| |||
|
|
Hi guys, how do you make a singleton access class? Do you know a better way of this one: [cut] Pure qui? :-)) |

|
se ti vede ZERO... |
|
ciao |
#6
| ||||||
| ||||||
|
|
how do you make a singleton access class? |
|
Do you know a better way of this one: |
|
var singletonClass = (function( ) { // Private variable var instance = null; |
|
// Private Constructor function myClass( ) { //... } return new function( ) |
|
{ this.constructor = null; this.getInstance = function( ) { if( ! instance ) { instance = new myClass( ); instance.constructor = null; |
|
} return instance; } } })( ); |
#7
| |||
| |||
|
|
Hi guys, how do you make a singleton access class? Do you know a better way of this one: var singletonClass = (function( ) { // Private variable var instance = null; // Private Constructor function myClass( ) { //... } return new function( ) { this.constructor = null; this.getInstance = function( ) { if( ! instance ) { instance = new myClass( ); instance.constructor = null; } return instance; } } })( ); |
#8
| ||||||||
| ||||||||
|
|
how do you make a singleton access class? How do you define a "singleton access class"? |
|
Do you know a better way of this one: The best way of doing something depends at least in part on what it is you are trying to do. |
|
var singletonClass = (function( ) { // Private variable var instance = null; There is little point in assigning null to this variable as its default undefined value will be just as false as the null value when you test it with - !instance -. |
|
// Private Constructor function myClass( ) { //... } return new function( ) I can think of no circumstances under which it makes sense to use the - new - operator with a function expression as its operand. In this case it would be simpler (and more efficient) to have an object literal returned at this point. That object could easily define - constructor - and - getInstance - properties. |
|
{ this.constructor = null; this.getInstance = function( ) { if( ! instance ) { instance = new myClass( ); instance.constructor = null; As you are using a constructor to create this object instance it would be possible to define the created object's - constructor property on the prototype of the constructor. Though there may be no good reason for using a contractor to create the object at all as you are only going to be creating one instance of the object, and so again a literal could be used instead. |
|
} return instance; } } })( ); The result of those changes could look like:- |
|
var singletonClass = (function( ){ // Private variable var instance; return ({ constructor:null, getInstance:function( ){ return ( instance|| ( instance = { constructor:null } ) ); } }); })(); |
|
- though that is almost certainly less than would be needed in any real context, but you will always suffer that if you don't define what it is exactly that you are trying to achieve. |
#9
| |||
| |||
|
|
how do you make a singleton access class? How do you define a "singleton access class"? i wanted a function/object which restricts the instantiation of one my object one time and which it ables one global access Do you know a better way of this one: The best way of doing something depends at least in part on what it is you are trying to do. A generic way to create a singleton object |
#10
| |||
| |||
|
|
IMHO, (new myClass()) can not be null|false|undefined.. |
![]() |
| Thread Tools | |
| Display Modes | |
| |