From: Garrett Smith on
Eric Bednarz wrote:
> Scott Sauyet <scott.sauyet(a)gmail.com> writes:
>
>> I'm pretty sure posters here had some reasonable critiques of using
>> this style to declare a singleton:
>
> I'm pretty sure I would like to know how and more than all why one would
> try to implement the singleton pattern in a class-free language.
>

That type of pattern allows an object to have private scope. That
particular pattern is useful when the program must have at most one of
that object, available immediately.

There are cases where you want to hide methods. Some prefer to use _ but
this has the disadvantage in that it isn't really private.

var foo = {
_fooFunc : function(a){
return a + 9;
},

fooFunc : function(a) {
return this._fooFunc(+a||0) + Math.random();
}
};

Pretend private makes it harder to refactor because once the method is
exposed, there is no guarantee of where it is being called from. ANd so
it might be better to have *real* private, like this:

var foo = new function(){
// Once, in the microcosm of foo...
function fooFunc(a) {
return a + 9;
}

this.fooFunc = function(a) {
return fooFunc(+a||0) + Math.random();
}
};

The benefit is that you get real private. Granted, there are some
security holes with arguments.caller, but that is mitigated here by
unary `+` operator.

Other benefits to using a closure here are that the identifiers will be
resolved more quickly and, if minification is used, will get munged to a
single letter, making them smaller and reducing the total file size,
both before and after gzip.
--
Garrett
comp.lang.javascript FAQ: http://jibbering.com/faq/
From: Garrett Smith on
Garrett Smith wrote:
> Eric Bednarz wrote:
>> Scott Sauyet <scott.sauyet(a)gmail.com> writes:
>>

[...]

>
> The benefit is that you get real private. Granted, there are some
> security holes with arguments.caller, but that is mitigated here by
> unary `+` operator.
>
make that "function.caller". There are some security holes with
function.caller.
--
Garrett
comp.lang.javascript FAQ: http://jibbering.com/faq/
From: Scott Sauyet on
On May 19, 7:46 pm, Eric Bednarz <bedn...(a)fahr-zur-hoelle.org> wrote:
> Scott Sauyet <scott.sau...(a)gmail.com> writes:
>> I'm pretty sure posters here had some reasonable critiques of using
>> this style to declare a singleton:
>
> I’m pretty sure I would like to know how and more than all why one would
> try to implement the singleton pattern in a class-free language.

Whether you call it the Singleton pattern or not, there are times when
I would like several functions that have a shared, but private state.
The easiest way I can think of doing that is encapsulating them in a
single object. Is there a better way to do that?

--
Scott
From: Thomas 'PointedEars' Lahn on
Garrett Smith wrote:

> Garrett Smith wrote:
>> The benefit is that you get real private. Granted, there are some
>> security holes with arguments.caller, but that is mitigated here by
>> unary `+` operator.
>
> make that "function.caller". There are some security holes with
> function.caller.

With `arguments.caller', too, as given a function with identifier `function'
(which cannot exist) if supported both would refer to the same object.

Function.prototype.caller is supported since JavaScript 1.0 and JScript 2.0.

arguments.caller is supported since JavaScript 1.1, but has been deprecated
since JavaScript 1.3, and was eventually removed in JavaScript 1.5. It was
never supported by JScript as such (in JScript 5.6 it refers to an object
that cannot be called even if the caller is a Function instance).

arguments.caller was never implemented by other known implementations and it
is likely that this applies to Function.prototype.caller, too. So far
neither feature can be considered safe-to-use (regardless of security
issues).

<https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Functions_and_function_scope/arguments/caller>
<http://msdn.microsoft.com/en-us/library/7t96kt3h%28VS.85%29.aspx>
<http://PointedEars.de/es-matrix#f> (to be updated accordingly)


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
From: John G Harris on
On Wed, 19 May 2010 at 17:01:52, in comp.lang.javascript, Garrett Smith
wrote:

<snip>
>That particular pattern is useful when the program must have at most
>one of that object, available immediately.
<snip>

If there are no classes, how can there be more than one of anything :-)

This illustrates one reason why I say that 'class' is too useful a word
to be restricted to one possibly future keyword.

John
--
John Harris