Singleton desing patter approach without function expressions...

  • Thread starter Luke Matuszewski
  • Start date
L

Luke Matuszewski

I am designing the library, which will hidden all its functions within
singleton object ... So for clients
they will use it like [functional_prefix].[function_name] eg.

system.getElementWithId('ruler');


At library side, i will use constructs like follows (at global scope)
eg.

function System() {

function _getElementWithId(id) {
/*
GENERIC CODE (using first DOM getElementById(id) and if not
successful
using document.all with some checks...)
*/
}

/*
MORE UTIL FUNCS
*/

this.getElementWithId = _getElementWithId;
/*
MORE ASSIGMENTS like
this.[util_method] = _[util_method];
*/

}

var system = new System();

System = null; /* since System constructor is desinged to produce
singletons, and it
should not be visible after singleton
instantiation... */



My question is simple, can i use construct like:

System = null;

, to redefine constructor System reference to null ? If not, then how i
should do it ?
Also remember that i can't use function expressions like

function(...) { }

I will appreciate somebody knowlegable to lighten my problem...

Best regards
Luke Matuszewski
 
M

Matt Kruse

Luke said:
I am designing the library, which will hidden all its functions within
singleton object ... So for clients
they will use it like [functional_prefix].[function_name] eg.

There is a better way to do what you're trying to do.
For example, this is simple:

var System = new Object();
System.getElementWithId = function() { }
System.otherFunction = function() { }
var system = new System();
System = null; /* since System constructor is desinged to produce
singletons, and it
should not be visible after singleton
instantiation... */

I don't believe that is a good example of the singleton pattern. Rather than
allowing multiple calls to a getInstance(), all of which return the same
instance, you're just destroying the constructor entirely. Why even bother?

Try this, for example:

var Singleton = (function() {
// This isn't "lazy" instantiation, but you could do that if you wanted
var instance = new Object();
instance.property = Math.random(1000);
return {'getInstance':function() { return instance; }}
})();

// The next line will generate a script error.
// You should not be able to directly instantiate an instance of the class
var object = new Singleton();

// The proper way to get the class instance
var x = Singleton.getInstance();
alert(x.property); // Alerts random number
var y = Singleton.getInstance();
alert(y.property); // Alerts same random number
alert(x==y); // True - same object
 
L

Luke Matuszewski

Matt said:
Oh, and what does that even mean?

Function expressions is expressions that returns function reference
(EcmaScript 3rd ed.- 13) like:

....
System.someFunc = function() {
/* CODE */
}
....

Function expressions where not present in EcmaScript 2nd and 1st
ed.(only function declarations where present). If i will not use
function expressions, then i could write code that
will run in all browsers (maybe without result, but also without errors
in old browsers if they encounter function expressions).
Also it seems that function expressions, in the form of

var System.someFunc = function identifier(...) { /* syntax allowed
since JavaScript 1.2 and JScript 1.0 */
}

, or in the form of

var System.someFunc = function(...) { /* syntax allowed since
JavaScript 1.5(!) and JScript ?(do not remeber) */
}

My only apprehension is question: can i null'yfi the function
identifier like:

function System() {
/* code */
}

var system = new System();
System = null; /* THIS */

, so System() constructor will not be visible after THIS statement.
(also see that if i can, there is only one object system, so no memory
leaks will happen in IE revelant to many execution contexts...).

Anyway, thanks for such knowlegable examples ;-)
Best regards
Luke Matuszewski
 
L

Luke Matuszewski

Matt said:

PS your example in http://www.JavascriptToolbox.com of moving <iframe>
is not strictly valid agains used doctype. Your are using:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

, and <iframe> is only allowed in loose dtd (see
http://www.w3.org/TR/html4/index/elements.html). To correct it, you
will have to use <object> instead of <iframe>... Thats all according to
specs, but according to real world the <iframe> tag is supported wider
by existing (existed) user agenst than the <object> tag (so maybe to
support all user agents there should be user-agent version checking...
but thats always bad idea).
See
<URL:http://www.cs.tut.fi/~jkorpela/html/iframe.html>
<URL:http://www.w3.org/TR/REC-html40/struct/objects.html#h-13.5>
<URL:http://www.w3.org/TR/2000/NOTE-WCAG10-HTML-TECHS-20000920/#alt-frames>
<URL:http://www.stack.nl/htmlhelp/faq/html/design.html#include-file>
<URL:http://www.webmasterworld.com/forum21/6539.htm>
<URL:http://www.webmasterworld.com/forum21/11590.htm>

To make a solution, on the page where you present moving <iframe>'s,
use:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

, as the table on <URL:http://hsivonen.iki.fi/doctype/> suggests (only
Konqueror 3.2 switches to quirk :( ). Also can also use strict doctype
inside iframe's, to maintain standards mode.
 
V

VK

Luke said:
var system = new System();
System = null; /* THIS */

IE (specially IE 5.x) chokes sometimes on nullifying function
references this way. Nothing to do with ECMA - just IE behavior. It is
more secure (and user friendly) to emulate the GetObject behavior thus
if an instance doesn't exists then create one; if an instance already
exists then return a reference to the existing instance.

function System() {
if (typeof System.instance == 'undefined') {
this.foo = ...
this.bar = ...
System.instance = this;
System.toString = function() {return 'function';}
}
else {
return System.instance;
}
}

// create instance:
var system = new System();

// this simply returns a reference to
// the existing instance:
var system2 = new System();

I also like to kill toString method for constructor (see above) to
prevent source code dump at runtime. Now alert(System) simply displays
"function". Not obligatory, but may be helpful.

Overall you have to remember that it is nothing like in say C++. In
compiled languages all these public/private/singleton/multiton :)
indeed produce compiled binary code with some predefined behavior, and
to change that behavior one has either to crack the code or to steal
the source. In JavaScript anyone can open your .js file and make all
your private members public or transform your singleton into an
auto-multiplying object.
So your concern should be about *runtime* only in two aspects only:
1) occasional environment damage (say some vital property overriden by
mistake due say a name conflict)
2) script highjaking (so your script is forced to act in a
non-desirable way or expose non-desirable property) - once again *at
runtime*
 
V

VK

Luke said:
PS your example in http://www.JavascriptToolbox.com of moving <iframe>
is not strictly valid agains used doctype. Your are using:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

Which is always a bad idea because strict.dtd is defective and never
was corrected since the original publication in 1999

It doesn't have <iframe> which alone makes it unusable in the modern
Web. Also it has some rude mistakes in form element definitions (like
missing "wrap" attribute for textarea).

Also it worth to point out that is someone is really crazy about
standard perfectness then HTML 4.01 should be connected with the
relevant DTD:
<http://www.w3.org/TR/html401/strict.dtd>

because <http://www.w3.org/TR/html4/strict.dtd> points to the previous
version (HTML 4.0)

It has no practical sense though because both
<http://www.w3.org/TR/html4/strict.dtd> and
<http://www.w3.org/TR/html401/strict.dtd> are exact copies of the very
same document (?!?!?)

Whoever is wondering what a hey they can try to reach someone Dave
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

Again the really correct version is:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html401/loose.dtd">

(with the same practical outcome - namely with a total absence of such)
 
M

Matt Kruse

Luke said:
Function expressions where not present in EcmaScript 2nd and 1st
ed.(only function declarations where present). If i will not use
function expressions, then i could write code that
will run in all browsers

If you can name a browser that is still in use that does not support
function expressions, you might have a point.
Writing code that still supports Netscape 2.0, for example, is a pointless
exercise.
 
L

Luke Matuszewski

VK napisal(a):
Which is always a bad idea because strict.dtd is defective and never
was corrected since the original publication in 1999

I must not agreed. For me standard that was accepted and largely
disscused at HTML ERB before formalization is good standard (also HTML
4.0 what certified in 1998 and HTML 4.01 is bugfixed version of that
HTML 4.0 standard defined in december of 1999).
It doesn't have <iframe> which alone makes it unusable in the modern
Web.
You can always use XmlHttpObject (AJAX) to make <iframe> emulation
Also it worth to point out that is someone is really crazy about
standard perfectness then HTML 4.01 should be connected with the
relevant DTD:
<http://www.w3.org/TR/html401/strict.dtd>

because <http://www.w3.org/TR/html4/strict.dtd> points to the previous
version (HTML 4.0)

It has no practical sense though because both
<http://www.w3.org/TR/html4/strict.dtd> and
<http://www.w3.org/TR/html401/strict.dtd> are exact copies of the very
same document (?!?!?)

Again i will not agree.
To answer why i suggested :

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

, i will point you to <URL:http://hsivonen.iki.fi/doctype/> (and if you
don't like to read much i will explain that with above doctype user
agents presented on table will "switch" themself to standards mode
(which means that they will try to use standards defined by w3c.org
concerning CSS and HTML when they will render the web page)... so for
me i will use:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

or even:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"> <!--Which allow me to use
<iframe> techniques-->

I know that you like IE (or are interested in IE), and so even on
microsoft site you can see my examples of doctype. See
Whoever is wondering what a hey they can try to reach someone Dave


Again the really correct version is:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html401/loose.dtd">

No. As you can see, even HTML 4.01 Recomendation specifies

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

or

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

See <URL:http://www.w3.org/TR/html401/struct/global.html#h-7.2> (which
is your HTML 4.01 Specification).

So first look inside specs before you will use words like 'no practical
sense' or 'defective'. I your words are true then organizations like:
MIT <URL:http://www.lcs.mit.edu/>,
INRIA <URL:http://www.inria.fr/>
Keio <URL:http://www.keio.ac.jp/>
and at last but not least W3C <URL:http://www.w3.org/>
, will have to be also with 'no practical sense' or 'defective'.

Best regards.
Luke M.
 
M

Matt Kruse

VK said:
Which is always a bad idea

Ridiculous.

Putting browsers into standards mode vs. quirks mode is definitely
recommended.
Just because the DTD doesn't allow iframes, for example, doesn't mean that
browsers won't allow them in the source and show them correctly. It just
means that it won't validate perfectly. Who cares. At least the browser is
in standards mode.
 
L

Luke Matuszewski

Matt Kruse napisal(a):
If you can name a browser that is still in use that does not support
function expressions, you might have a point.
Writing code that still supports Netscape 2.0, for example, is a pointless
exercise.

Agree. From my point of view, scripts that work down to Netscape 4.0x
(in a sense that they do not do harm, including memory leaks provided
by circular references between ActiveX objects and DOM objects in IE)
are well written. From this point (Netscape 4.0x) you can always use
function expression syntax below:

System.myFunc = function
identifierOnlyForReferencingInsideThisFunction() {

}

(the syntax: function(...) { } is only supported from JavaScript
1.5(Netscape 6.x), thus should be avoided if writting scripts that
should run on them too).

Again ***thanks*** for knowlegable examples from Matt Kruse and VK.

Best regards.
Luke M.

PS Matt you can update your http://www.AjaxToolbox.com to suppor
IceBrowser see <URL:http://www.jibbering.com/2002/4/httprequest.html>
(window.createRequest)
 
J

Jim Ley

Ridiculous.

Putting browsers into standards mode vs. quirks mode is definitely
recommended.
Just because the DTD doesn't allow iframes, for example, doesn't mean that
browsers won't allow them in the source and show them correctly. It just
means that it won't validate perfectly. Who cares. At least the browser is
in standards mode.

Absolutely, if the UA's are going to play silly buggers with standards
and quirks mode, you've got to play along.

Jim.
 
L

Luke Matuszewski

Matt Kruse napisal(a):
Putting browsers into standards mode vs. quirks mode is definitely
recommended.
Just because the DTD doesn't allow iframes, for example, doesn't mean that
browsers won't allow them in the source and show them correctly. It just
means that it won't validate perfectly. Who cares. At least the browser is
in standards mode.

You can also use:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"> <!-- you can use <iframe> !
;-) -->

(doctype with URL), and all browser (almost - the exception is
Konqueror 3.2 but i am almost sure that they will change this to
"almost standards mode" - which common for IE6, Mozilla (Gecko),
Safari... when used with Transitional).
See <URL:http://hsivonen.iki.fi/doctype/> and
<URL:http://gutfeldt.ch/matthias/articles/doctypeswitch/table.html>
(so Transitional with System URI is really standards or almost
standards mode).

Best regards
Luke Matuszewski
 
L

Luke Matuszewski

Luke Matuszewski napisal(a):
You can also use:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
^^^^
"http://www.w3.org/TR/html4/loose.dtd"> <!-- you can use <iframe> !
;-) -->
sorry my fault, you actualy can use:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd"> said:
(doctype with URL), and all browser (almost - the exception is
Konqueror 3.2 but i am almost sure that they will change this to
"almost standards mode" - which common for IE6, Mozilla (Gecko),
Safari... when used with Transitional).
See <URL:http://hsivonen.iki.fi/doctype/> and
<URL:http://gutfeldt.ch/matthias/articles/doctypeswitch/table.html>
(so Transitional with System URI is really standards or almost
standards mode).

Best regards
Luke Matuszewski
 
V

VK

Jim said:
Absolutely, if the UA's are going to play silly buggers with standards
and quirks mode, you've got to play along.

Declare Strict mode knowing in advance that you're going to break it is
a pure hypocrisy. If you need a standard-compliant mode from IE (for
say uniformed element offset calculations) then use

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html401/loose.dtd">

P.S. In some higher education establishments I've seen even more
ridicilous approach: people were making iframe-less HTML with iframes
generated and inserted onload via JavaScript (document.createElement).
I've seen one prof who was debugging his iframe behavior while
periodically validating the page against the 4.01 Strict.
I see *he was happy* by seeing that both iframe is working properly and
4.01 Strict is validated. I'm not sure is it a new XX! century
psychosis or simply a childish behavior.
In any case it would be nice to see the majority of developers acting
normal and adult-like.
 
L

Luke Matuszewski

Furthermore "almost standards mode" is "standars mode" with addition to
this:
<quote>
"Almost standards" rendering mode is exactly the same as "standards"
mode in all details save one: the layout of images inside table cells
is handled as they are in Gecko's "quirks" mode, which is fairly
consistent with other browsers, such as Internet Explorer. This means
that sliced-images-in-tables layouts are less likely to fall apart in
Gecko-based browsers based on the rendering engine found in Mozilla
1.0.1 or later when in either "quirks" or "almost standards" mode. (See
the DevEdge article "Images, Tables, and Mysterious Gaps" for a
detailed explanation of how such layouts are treated in "standards"
mode.)
</quote>
<URL:http://developer.mozilla.org/en/docs/Gecko's_"Almost_Standards"_Mode>

BR
Luke Matuszewski
 
V

VK

Luke said:
, i will point you to <URL:http://hsivonen.iki.fi/doctype/> (and if you
don't like to read much i will explain that with above doctype user
agents presented on table will "switch" themself to standards mode
(which means that they will try to use standards defined by w3c.org
concerning CSS and HTML when they will render the web page)... so for
me i will use:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

You did not get my point. DTD declares used scheme in the first
segment, and DTD file containing the scheme in the second segment. The
second part is needed if say browser has no clue what HTML 4.01 is:-
then it can retrieve the scheme from the file to validate.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

is a *contradictory invalid* declaration because in the first segment
you declare HTML 4.01, but you propose as the validation scheme file
HTML 4.0 <http://www.w3.org/TR/html4/strict.dtd>

It doesn't have sense from any point of view. If you want HTML 4.01,
then please provide the relevant DTD file:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html401/strict.dtd">

(this file exists and waiting for you)
I know that you like IE (or are interested in IE)

My position is clearly expressed in
Message-ID: <[email protected]>

So no, I'm not Microsoft biased but at the same time "Microsoft must
die" is not my post auto-signature :)

It is not related though to the question of matching the scheme
declaration to the scheme file.
For HTML 4.0 we're using one file
For HTML 4.01 we're using other file

The fact that this contradiction was not noticed for so many years (and
the relevant <http://www.w3.org/TR/html401/> folder stayed without use)
is just another proof of how "serious" all this DTD story is.
 
T

Thomas 'PointedEars' Lahn

Matt said:
Agreed.

Putting browsers into standards mode vs. quirks mode is definitely
recommended.

Non sequitur.
Just because the DTD doesn't allow iframes, for example, doesn't mean that
browsers won't allow them in the source and show them correctly. It just
means that it won't validate perfectly. Who cares.

Obviously you do not. Other people do because they care for _interoperable_
markup, that is, markup that has the potential to work everywhere and
anytime (for the foreseeable future), not markup that works only in a few
versions of a few user agents.
At least the browser is in standards mode.

Nonsense. Standards Compliance Mode does not require a Strict DTD. HTML
4.01 Transitional also triggers this mode and allows for the `iframe'
element. Furthermore, HTML 4.01 Strict provides the `object' element as
standards-compliant alternative to `iframe'.


PointedEars
 
L

Luke Matuszewski

VK napisal(a):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

is a *contradictory invalid* declaration because in the first segment
you declare HTML 4.01, but you propose as the validation scheme file
HTML 4.0 <http://www.w3.org/TR/html4/strict.dtd>

It doesn't have sense from any point of view. If you want HTML 4.01,
then please provide the relevant DTD file:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html401/strict.dtd">

(this file exists and waiting for you)

There is some true in your words i quoted above, but my concern is
not valid DTD syntax but valid behaviour of user agents which is stated
on following sites:
<URL:http://gutfeldt.ch/matthias/articles/doctypeswitch/table.html>
<URL:http://hsivonen.iki.fi/doctype/>
<URL:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnie60/html/cssenhancements.asp>
, and they ***all*** have followin DTD:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

, or

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

(both switch revelant UAs into almost or standads mode)

BR
Luke Matuszewski
 
L

Luke Matuszewski

VK napisal(a):
The fact that this contradiction was not noticed for so many years (and
the relevant <http://www.w3.org/TR/html401/> folder stayed without use)
is just another proof of how "serious" all this DTD story is.

See: <URL:http://www.w3.org/TR/html401/struct/global.html#h-7.2> of
HTML 4.01 specifiction also... you there have followin examples:
<quote>
HTML 4.01 specifies three DTDs, so authors must include one of the
following document type declarations in their documents. The DTDs vary
in the elements they support.

* The HTML 4.01 Strict DTD includes all elements and attributes
that have not been deprecated or do not appear in frameset documents.
For documents that use this DTD, use this document type declaration:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

* The HTML 4.01 Transitional DTD includes everything in the strict
DTD plus deprecated elements and attributes (most of which concern
visual presentation). For documents that use this DTD, use this
document type declaration:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

* The HTML 4.01 Frameset DTD includes everything in the
transitional DTD plus frames as well. For documents that use this DTD,
use this document type declaration:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
"http://www.w3.org/TR/html4/frameset.dtd">
</quote>

Interesting fact is that when reading more of it you have links to
revelant files:
<quote>
The URI in each document type declaration allows user agents to
download the DTD and any entity sets that are needed. The following
(relative) URIs refer to DTDs and entity sets for HTML 4:

* "strict.dtd" -- default strict DTD (points to:
http://www.w3.org/TR/html401/strict.dtd)
* "loose.dtd" -- loose DTD (points to:
http://www.w3.org/TR/html401/loose.dtd)
....
</quote>

BR
Luke Matuszewski
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top