473,388 Members | 870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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 2004
"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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.