From: Thomas 'PointedEars' Lahn on
Scott Sauyet wrote:

> Asen Bozhilov wrote:
>> I want Singleton with properties:
>> - Doesn't have constructor
>> - Create instance properties when i call `Singleton.getInstance()`
>> - Create instance properties only once
>>
>> var x = Singleton.getInstance();
>> window.alert(typeof x.method1); //function
>> window.alert(typeof x.method2); //function
>> window.alert(Singleton.getInstance().method1 === x.method1); //true
>
> This looks more complicated than necessary, but I think would work:
>
> Singleton = (function() {
> var instance = null;
> return {
> getInstance: function() {
> if (instance == null) {
> instance = {
> method1: function() {},
> method2: function() {},
> }
> }
> return instance;
> }
> }
> })();
>
> Are there simplifications to be done here?

Only optimizations: Lose the trailing comma (increases compatibility), and
end statements and assignments with semicolon to distinguish them from
/Block/ statements (Pretty Printing).

And I cannot stress this too much: DO NOT do the whole nonsense in these
languages. You only need the singleton pattern where objects have no
initializer syntax, like in Java. Implementations of ECMAScript Ed 3.
forward instead allow

var instance = {
method1: function() {},
method2: function() {}
};


PointedEars
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$8300dec7(a)news.demon.co.uk> (2004)
From: Garrett Smith on
Scott Sauyet wrote:
> On Jan 25, 3:50 pm, Asen Bozhilov <asen.bozhi...(a)gmail.com> wrote:
>> I want Singleton with properties:
>> - Doesn't have constructor

Why not?

>> - Create instance properties when i call `Singleton.getInstance()`
>> - Create instance properties only once
>>
>> var x = Singleton.getInstance();
>> window.alert(typeof x.method1); //function
>> window.alert(typeof x.method2); //function
>> window.alert(Singleton.getInstance().method1 === x.method1); //true
>
> This looks more complicated than necessary, but I think would work:
>
[snip]
It doesn't look complicated at all.

>
> Are there simplifications to be done here?
>

I added var, removed the extra comma, and added a couple of semicolons
that were omitted.

I also removed the null assignment and null check.

var Singleton = (function() {
var instance;
return {
getInstance : function() {
if(!instance) {
instance = {
method1: function() {},
method2: function() {}
};
}
return instance;
}
};
})();
--
Garrett
comp.lang.javascript FAQ: http://jibbering.com/faq/
From: Scott Sauyet on
On Jan 25, 4:39 pm, Asen Bozhilov <asen.bozhi...(a)gmail.com> wrote:
> Scott Sauyet wrote:
> > This looks more complicated than necessary, but I think would work:
>
> But if i want my `Singleton` inherit from something, I'll be need to
> modified my `getInstance' method. I don't want to do it this. In
> `getInstance' i want to create only instance properties. I'll be
> implement in:

I don't follow this. Do you have an example of how you'd want to use
it?


> var Singleton = {};
> Singleton.getInstance = function()
> {
>   this.method1 = function(){};
>   this.method2 = function(){};
>
>   this.getInstance = function(){
>     return this;
>   };
>   return this;
>
> };

This doesn't make any sense to me. What is "this" here?

-- Scott
From: Scott Sauyet on
On Jan 25, 5:46 pm, Thomas 'PointedEars' Lahn <PointedE...(a)web.de>
wrote:
> And I cannot stress this too much: DO NOT do the whole nonsense in these
> languages.  You only need the singleton pattern where objects have no
> initializer syntax, like in Java.  Implementations of ECMAScript Ed 3.
> forward instead allow
>
>   var instance = {
>     method1: function() {},
>     method2: function() {}
>   };

I agree that there is rarely if ever a need for the Singleton pattern
in the classic OO sense here.

But the example above doesn't match one of the OP's requirements:

| Create instance properties when i call `Singleton.getInstance()`

I'm not sure I see much of a reason for this requirement, though,
unless there's some very heavy-weight work going on in the creation of
the instance *and* it fairly frequently is not needed.

-- Scott
From: Scott Sauyet on
On Jan 25, 8:00 pm, Garrett Smith <dhtmlkitc...(a)gmail.com> wrote:
> I added var, removed the extra comma, and added a couple of semicolons
> that were omitted.
>
> I also removed the null assignment and null check.

The comma was just a cut-and-paste error, but the rest were real
problems in my quicky implementation. Thanks.

-- Scott