473,473 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Simpleton singleton that installs its own function type properties

foo() generates elements with event handlers that invoke foo function
properties.

Is this an abhorrent or misthought pattern ?
It allows just the one occurence of identifier /foo/ to be changed to
/whatever/ when need arises and everything should still work.

function foo () {
var callee = arguments.callee
if (callee.singleton != undefined)
return callee.singleton

if (this == window)
return new callee(arguments)

installClassFunctions()

callee.singleton = this
return

function installClassFunctions() {
callee.classMethod1 = function () {alert('1')} // global function but
property of foo
callee.classMethod2 = function () {alert('2')}
}
}

foo()
foo.classMethod1() // not normally invoked directly, verifies operation

Even though foo is singleton, the handlers (in question) would have to be
passed a global reference to the singleton instance (if the classMethods
were programmed to be instance of foo properties. (Also, no sense in
prototyping singleton methods)

--
Richard A. DeVenezia
Jul 20 '05 #1
1 2009
"Richard A. DeVenezia" <ra******@ix.netcom.com> wrote in message
news:bj************@ID-168040.news.uni-berlin.de...
foo() generates elements with event handlers that invoke
foo function properties.
Not in this code it doesn't.
Is this an abhorrent or misthought pattern ?
How well any given pattern fits the situation it is going to be used in
depends a lot on what it is intended to achieve. As your script does
nothing concrete it is difficult to say whether it is the best way of
going about it (beyond the trivial observation that the best way of
doing nothing is not to write any code ;-)
It allows just the one occurence of identifier /foo/ to be changed
to /whatever/ when need arises and everything should still work.
Referring to - arguments.calee - seems reasonable way of referring to
the constructor anonymously. But is the ability to change the identifier
for the constructor without having to do a search and replace on the
rest of the code is important then there are other ways of achieving
that.
function foo () {
var callee = arguments.callee

if (callee.singleton != undefined)
return callee.singleton

if (this == window)
return new callee(arguments)

installClassFunctions()

callee.singleton = this
return

function installClassFunctions() {
callee.classMethod1 = function () {alert('1')} // global function butproperty of foo
With newsgroup postings it is often worth looking at your newsreader
software's line wrapping settings (mine is set at 72 characters, for
example) and pre-wrapping source code so that it will not be split
across lines in undesirable places. JavaScript is very tolerant of the
insertion of white space characters.
callee.classMethod2 = function () {alert('2')}
}
}

foo()
foo.classMethod1() // not normally invoked directly, verifies operation
Bear in mind that in a function invoked as a method of a function the -
this - keyword becomes a reference to the function that the invoked
function is a method of..
Even though foo is singleton, the handlers (in question) would have to
be passed a global reference to the singleton instance (if the
classMethods were programmed to be instance of foo properties.
As it stands this singleton pattern is one perfectly good approach.
There are at least half a dozen alternatives and this one would not be
my first choice of them. But my choice would be influenced most by those
event handlers that you did not include. How they are assigned, what
they do, what they need to refer to and so on.
(Also, no sense in prototyping singleton methods)


There may be some sense in it (the prototype object exist after all), or
assigning the methods directly to properties of the one object when
constructed. But if the singleton is only interested in event handlers
does it need a public interface at all? One of the main issues with
assigning inner functions to event handlers is the duplication of
function objects; that is not going to be an issue with a singleton.
(There is also the IE memory leak issue but that can be avoided with
care.)

The hypothetical discussion of coding patterns is one thing but there
comes a point where code is written to address a requirement. Your
requirement seems to include a desire to make the singleton "class"
independent of the identifier for its constructor, but in addition you
have associated requirements in connection with these event handlers,
and they should be expected to influence the decision as to the best
pattern to apply.

Richard.
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Gordon | last post by:
I'm just getting started in ASP and can't seem to figure out how to run a function in ASP. I get an error of "Line 11 - Type Mismatch:'myvalidate'. Line 11 is the Input Button. Here is my simple...
3
by: Steve | last post by:
Guys, I've written a custom control (or at least tweaked someone else's!) in C# to be used on forms (rather than asp.net forms). The control is a date time picker, but its Value property returns...
3
by: Jonathan Turkanis | last post by:
Can the syntax for function types be used within a function type declaration as a synonym for the corresponding pointer-to-function type? For instance, is int(char(long)) a valid type...
5
by: Lionel B | last post by:
Greetings, I am trying to implement "element-wise" arithmetic operators for a class along the following lines (this is a simplified example): // ----- BEGIN CODE ----- struct X { int a,b;
2
by: ABC | last post by:
I am always to write clases which has properties, is here any best tools or method let me typing properties codes more fast?
4
by: jrx | last post by:
I've got my_error exception type, defined as: class my_error : public std::runtime_error { public: my_error(const std::string& msg) : std::runtime_error(msg) {} }; Then, I've got Wrapper...
6
by: cppnow | last post by:
Hello. I have a strange conceptual problem I'm trying to think about. I would like to build a library that allows the user to do the following: 1) User defined types: The user defines their...
5
by: eros | last post by:
<html> <head> <SCRIPT type="text/javascript"> <!-- Beginning of JavaScript - //追加 button function Tsuika_onclick(strbtn){ alert(strbtn); // alert1 ...
3
by: sh.vipin | last post by:
In the following program, with gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-56) there is no error. I have two queries. 1. isn't it illegal to apply sizeof on function type. 6.5.3.4 The...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.