From: "Michael Haufe ("TNO")" on
From looking at the code below:
--------------------------------------------------
var Singleton1 = (function(){
//...
return {
foo : function(){
//...
},
bar : function(){
//...
}
};
})();

var Singleton2 = new function(){
//...
this.foo = function(){
//...
};
this.bar = function(){
//...
};
};
-----------------------------------------------

Is there a good reason why jslint should complain about Singleton2,
but not Singleton1?

jslint message:
-----------------------------------------------
Problem at line 13 character 18: Weird construction. Delete 'new'.

var Singleton2 = new function(){

Problem at line 21 character 2: Missing '()' invoking a constructor.

};
-----------------------------------------------
From: Richard Cornford on
On Jan 25, 4:24 am, Michael Haufe (\"TNO\") wrote:
> From looking at the code below:
> --------------------------------------------------
> var Singleton1 = (function(){
> //...
> return {
> foo : function(){
> //...
> },
> bar : function(){
> //...
> }
> };
>
> })();
>
> var Singleton2 = new function(){
> //...
> this.foo = function(){
> //...
> };
> this.bar = function(){
> //...
> };};
>
> -----------------------------------------------
>
> Is there a good reason why jslint should complain about
> Singleton2, but not Singleton1?
<snip>

I wouldn't be surprised if the motivation followed from years of
seeing the - new - operator used for no reason other than to execute
the function. Examples were the new object created by implication was
never employed in the function body and so its creation (and setting
up ([[Prototype]] assignment, etc)) represented pure overhead. Where
just calling the function would achieve the same outcome without the
overheads, without the confusion of implying an object construction
process to the reader, and without the use of any more source code
characters.

Richard.
From: Asen Bozhilov on
Michael Haufe (TNO) wrote:
> From looking at the code below:
> --------------------------------------------------
> var Singleton1 = (function(){
> [...]
> })();
>
> var Singleton2 = new function(){
> [...]
> };

Both solutions allocate memory for instance properties, before i need
from your `Singleton` object.
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

How can i implement?
From: Scott Sauyet on
On Jan 25, 3:50 pm, Asen Bozhilov <asen.bozhi...(a)gmail.com> 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?

-- Scott
From: Asen Bozhilov on
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:

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

this.getInstance = function(){
return this;
};
return this;
};