From: Asen Bozhilov on
Scott Sauyet wrote:
> Richard Cornford wrote:
>
> > But if you are trying to guard against the random scrubbings of fools
> > and halfwits what are you going to do to prevent someone re-assigning
> > a new object with its own - getInstance - method to - Singleton -?
>
> No, of course you can't safeguard everything.  Asen asked for advice
> on how to do something, and specifically asked for

I think Richard maintain something other. Unreasonable usage of
provided code. Everything have requirements for proper and optimize
usage. If i have car which fuel is benzine, if i put diesel i cannot
expect from my car to move. Moreover my car will be broken after my
unreasonable usage. Of course my car has user manual with suggestions
for proper usage. Just like code. If code have documentation, i am
expect developers follow documentation and my suggestions about my
code.

> | I want Singleton with properties:
> | - Doesn't have constructor

> which, as already pointed out, is a strange requirement.

I don't have any benefits from constructor function in my `Singleton`.
The whole "nonsense" idea for `getInstance' method, is to create
instances properties, when i explicit call `getInstance' method. That
can be benefit when i don't want to allocate memory for all properties
of my `Singleton` before i use my `Singleton`.

Thanks for responses.
Regards.
From: Scott Sauyet on
Asen Bozhilov wrote:
> Scott Sauyet wrote:
>> Richard Cornford wrote:
>
>>> But if you are trying to guard against the random scrubbings of fools
>>> and halfwits what are you going to do to prevent someone re-assigning
>>> a new object with its own - getInstance - method to - Singleton -?
>
>> No, of course you can't safeguard everything.  Asen asked for advice
>> on how to do something, and specifically asked for
>
> I think Richard maintain something other. Unreasonable usage of
> provided code. Everything have requirements for proper and optimize
> usage. [ ... ] If code have documentation, i am
> expect developers follow documentation and my suggestions about my
> code.

I do understand that. And I understood it before Richard posted it.
It would be stupid to try to use the code in this manner. But your
list of required properties made it sound as though an exposed
constructor might cause problems. If you were in total control of how
your code is called, there would presumably be no need for that
requirement. That's why I went further with the constructor
examples. If you don't really need that, the differences between the
approach you give and the one I do are as much a matter of style as
anything else, I believe.


>> | I want Singleton with properties:
>> | - Doesn't have constructor
>> which, as already pointed out, is a strange requirement.
>
> I don't have any benefits from constructor function in my `Singleton`.

If it's simply that the constructor is not needed, this probably
shouldn't be listed as a requirement, but left as an implementation
detail.


> The whole "nonsense" idea for `getInstance' method, is to create
> instances properties, when i explicit call `getInstance' method. That
> can be benefit when i don't want to allocate memory for all properties
> of my `Singleton` before i use my `Singleton`.

Either technique will perform this. In yours the properties are
stored in the Singleton object, in mine they are stored in the
"instance" variable stored in the closure, but in either case, they
are where you really need them to be, in the result of
Singleton.getInstance();

Cheers,

-- Scott
From: Garrett Smith on
Asen Bozhilov wrote:
> Scott Sauyet wrote:
>> Richard Cornford wrote:
>>
>>> But if you are trying to guard against the random scrubbings of fools
>>> and halfwits what are you going to do to prevent someone re-assigning
>>> a new object with its own - getInstance - method to - Singleton -?
>> No, of course you can't safeguard everything. Asen asked for advice
>> on how to do something, and specifically asked for
>
> I think Richard maintain something other. Unreasonable usage of
> provided code. Everything have requirements for proper and optimize
> usage. If i have car which fuel is benzine, if i put diesel i cannot
> expect from my car to move. Moreover my car will be broken after my
> unreasonable usage. Of course my car has user manual with suggestions
> for proper usage. Just like code. If code have documentation, i am
> expect developers follow documentation and my suggestions about my
> code.
>
>> | I want Singleton with properties:
>> | - Doesn't have constructor
>
>> which, as already pointed out, is a strange requirement.
>
Not necessarily strange. It might have been that the goal is to hide the
constructor, to prevent two instances of `Singleton` from being created.

> I don't have any benefits from constructor function in my `Singleton`.
> The whole "nonsense" idea for `getInstance' method, is to create
> instances properties, when i explicit call `getInstance' method. That
> can be benefit when i don't want to allocate memory for all properties
> of my `Singleton` before i use my `Singleton`.
>
Singleton is appropriate when the program needs at most one.

A factory getInstance method can facilitate that.
--
Garrett
comp.lang.javascript FAQ: http://jibbering.com/faq/
From: Garrett Smith on
Thomas 'PointedEars' Lahn wrote:
> Garrett Smith wrote:
>
>> 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?
>
> The question is pointless; it simply cannot be done. Every object has a
> constructor, even those created with initializers.
>

Not every object will have constructor.

Even if changed to "Every native object", it would still not be true.

Even if changed to "Every object except the global object," it would
still not be true.

A new'd object automatically gets a [[Prototype]] from the object that
constructed it. The constructor comes from that. The global object's
[[Prototype]] is implementation dependent, so it might have any constructor.

(function(){
var a = [];

function F(){}

var i = new F;
a.push(i.constructor); // F

delete F.prototype.constructor;

// look up prototype chain.
a.push(i.constructor); // Object

delete Object.prototype.constructor;
a.push("constructor" in i); // false

return a;
})();

Results: [F(), Object(), false]

As a bookmarklet:

javascript: alert(function(){var a = [];function F(){}var i = new
F;a.push(i.constructor);delete
F.prototype.constructor;a.push(i.constructor);delete
Object.prototype.constructor;a.push("constructor" in i);return a}())

In the following code:
var i = new Singleton();
i.constructor

The constructor property is resolved up the prototype chain of `i`. If
Object.prototype.constructor has been deleted, then the result is
`undefined`.

javascript: alert([delete Object.prototype.constructor, "constructor" in
{}]);

An object having a constructor isn't a problem. I do not recommend
deleting Object.prototype.constructor. The example code demonstrated
something that is allowed by the language, not to be taken as practical
advice.
--
Garrett
comp.lang.javascript FAQ: http://jibbering.com/faq/
From: Thomas 'PointedEars' Lahn on
Garrett Smith wrote:

> Thomas 'PointedEars' Lahn wrote:
>> Garrett Smith wrote:
>>> 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?
>> The question is pointless; it simply cannot be done. Every object
>> has a constructor, even those created with initializers.
>
> Not every object will have constructor.

True.

> Even if changed to "Every native object", it would still not be true.

True, the Global Object makes an exception there. There is no description
in the Specification how it is created; it is simply "created before
control enters any execution context" and then it "is there".

> Even if changed to "Every object except the global object," it would
> still not be true.

Yes, it would. While maybe not accessible from ECMAScript code, host
objects need to have a constructor somewhere by which they have been
constructed.

> A new'd object automatically gets a [[Prototype]] from the object that
> constructed it. The constructor comes from that.

No, the constructor needs to exist before.

> The global object's [[Prototype]] is implementation dependent, so it
> might have any [[constructor. [...]

I was not talking about the `constructor' property. You miss the point.

However, I should have said "every *user-defined* object". I assumed
that to be obvious from the context of the statement and my mentioning
initializers; my mistake.


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7(a)news.demon.co.uk>