473,811 Members | 2,706 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Namespaces, or getting function foo.bar() declarations to work

Hi!

I don't think I've ever been here, so I assume I'm addressing a bunch of
nice people. Can you help me? I don't think there's a solution, but who
knows.

The thing is, picture a large project where you want some encapsulation.
But a lot of it being grouping of what would otherwise be static members
of the global namespace. So that instead of

fooEcks(), fooWye, fooZed()

you can have

foo.ecks(), foo.wye(), foo.zed()

with foo being a global variable.
Now this is perfectly possible, that I know, by declaring

var foo={} OR function foo(){}

and then having

foo.ecks=functi on(args){body}

or then by defining them inline, as in

var foo={ecks: function(args){ body}}

However, in all of these, the functions created are anonymous. This may
make no difference, but I don't like it and it impacts debugging, when
you're trying to determine their name.

You can given them a name, as in

foo.ecks=functi on ecks(args){body }

But this is error prone, since you have to keep the two names in sync,
and that is a sure road to damnation.
It turns out that in IE6, if you do

function foo(){}; // with 'var foo' it won't work
function foo.ecks(args){ body};

the browser does create a member of foo, named ecks, which is then your
foo.ecks function. I may be missing something, but this seems great to
me, in absence of alternatives (my preferred would be simply that
internal named functions were public).

But as it is, neither Opera nor Gecko accept this notation. And for the
life of me, I haven't been able to figure out any other way to define a
public static member (though instance members would have the same
problem) that isn't anonymous without referring its name twice. Needless
to say, something like

var foo={ecks: fooEcks};
function fooEcks(args){b ody};

isn't the solution because it keeps the repetition (though this time it
could be automated to avoid errors - but notice that then we're going
far away from the natural constructs of the language), clutters the
global namespace anyhow, and probably creates all sorts of scope
problems (otherwise, we may just delete all of those references after
reassigning the functions to their namespace, but somehow this doesn't
look like a sensible solution).

So, any ideas? Thanks,
--
am

laurus : rhodophyta : brezoneg : smalltalk : stargate
May 25 '06
14 1652
Richard Cornford wrote:
However, in all of these, the functions created are
anonymous. This may make no difference, but I don't
like it and it impacts debugging, when you're trying
to determine their name.

This assumption appears to be the crux of your issue, but
you have not explained or justified it. Most javascript
error reports output line numbers (at minimum) so I don't
see any need to have a name associated with the function
for debugging.


It's not an assumption, they really are anonymous. ...


That was not the assumption. The assumption was that using anonymous
functions would impact debugging. It is an assumption as anonymous
functions are common on non-trivial javascript and if their use really
was a problem when debugging that would have been noticed by now.


I said why it impacts debugging, if no one else is bothered by that is
another matter. For all I know, people are tearing their hair off for
the short supply of great javascript IDEs. I don't think it would hurt
that a name could be determined for every function (if not their own,
that of the variable they are assigned to, or that of the scope object).
function foo()
{
function bar()
{
debug('hello', 1, 'a')
}
}

And function debug would output something like

'10:04:55 [foo] hello, 1, a'


The 'hello' got into that output because you put it there, why not put
in the information that tells you what you need in the output, like
something that tells you the context of the debug call?


Only because it would have to be done every time. Given the dynamic
nature of javascript, it's even possible that 'bar' is bound to other
scope, so maybe something like

'10:04:55 [foo > bar] hello, 1, a'
'10:04:57 [dif > bar] hello, 1, a'

would be even more interesting. The next version of ECMAScript could
surely define some more meta- functionality for functions, something
that could be put to good use by IDEs.
--
am

laurus : rhodophyta : brezoneg : smalltalk : stargate
May 25 '06 #11
António Marques <m.**@sapo.pt > writes:
Hey, this looks great! But will the final code behave just in the same
way as if it were defined with

var ns = {accumulator:[]};
ns.add = function add(n) { ... };
ns.sum = function add(n) { ... };


It should, yes. There are no other scopes involved.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
May 25 '06 #12
António Marques wrote:
RobG wrote:
It turns out that in IE6, if you do

function foo(){}; // with 'var foo' it won't work
This line should work with `var foo' as well, but the next line should
not work anyway.
function foo.ecks(args){ body};

the browser does create a member of foo, named ecks, which is then
your foo.ecks function.
It works only in IE6 this way because it uses a proprietary extension of
ECMAScript (or an implementation flaw). The FunctionDeclara tion production
requires the `function' keyword to be followed by whitespace, followed by
an _identifier_ (not by a property accessor in dot notation), followed by
the parameter list followed by the function body. Therefore, the standards
compliant way is

function foo() {}

or a FunctionExpress ion, where the identifier after the `function' keyword
is optional:

var foo = function() {};

And then:

foo.ecks = function(args){ /* body */ };

Because `foo' is always a reference to a Function object that can be
augmented, like any native object:

var foo = {}; // or `= new Object()'
foo.ecks = function(args){ /* body */ };

or

var foo = {
ecks: function(args){ /* body */ };
};
I may be missing something, but this seems great to me,
Even if written standards compliant, it has its drawbacks. One is that
you need another function object. Another one, that I have pointed out
recently, is that if you use the Function object as a constructor,
objects created with it will not inherit the ecks() method; you have to
augment the constructors prototype object to achieve that.
in absence of
alternatives (my preferred would be simply that internal named functions
were public).


Do you mean:

function foo(){
function bar(){}
}
bar(); // Error: bar is not defined.


( Actually foo.bar() )


foo.bar() in this context works only in JavaScript 1.5 due to a proprietary
extension of ECMAScript (or an implementation flaw). With the advent of
Gecko 1.8b2+ based UAs, like Firefox 1.5, that use JavaScript 1.6, it has
become not only a deprecated practice but also an obsolete one.
PointedEars

P.S.
Please don't remove attribution lines for quoted text that related to them.
--
A man who works with his hands is a laborer; a man who works with his
hands and his brain is a craftsman; but a man who works with his hands
and his brain and his heart is an artist.
-- Louis Nizer, lawyer (1902-1994)
May 26 '06 #13
pe**********@gm ail.com wrote:
António Marques wrote:
you can have

foo.ecks(), foo.wye(), foo.zed()
Richard explained to me just yesterday that this is a bad idea because
JavaScript is not compiled but rather interpreted.


Probably you misunderstood, since JavaScript is (JIT-)compiled, then
interpreted. Compiled vs. interpreted language is not the issue, but
the moment of evaluation of identifiers is:
He wrote

"Simulated namespaces are a very questionable notion. In other language
the namespaces are resolved when the code is compiled. Javascript must
resolve those long property accessors each and every time they are
used."

PointedEars
--
This is Usenet. It is a discussion group, not a helpdesk. You post
something, we discuss it. If you have a question and that happens to get
answered in the course of the discussion, then great. If not, you can
have a full refund of your membership fees. -- Mark Parnell in alt.html
May 26 '06 #14
Hi,

Thank you all for your help on this matter.
--
am

laurus : rhodophyta : brezoneg : smalltalk : stargate
May 29 '06 #15

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

Similar topics

18
3055
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of a single collections type, I should be proposing a new "namespaces" module instead. Some of my reasons: (1) Namespace is feeling less and less like a collection to me. Even though it's still intended as a data-only structure, the use cases...
15
5433
by: Stuart | last post by:
I work in a small company with developers who do not like to use "new" features when they find the old ones sufficient. e.g. given a choice between a class and a namespace, they'll pick class. Given a choice between naming functions at the global scope with subsystem prefixes (e.g. CTNode, CPNode) vs. namespaces (Time::Node, Place::Node) we'll use the global namespace. Prefixes they understand, and prefixes are short and do the job. ...
17
2080
by: clintonG | last post by:
Using 2.0 with Master Pages and a GlobalBaseClass for the content pages. I understand the easy part -- the hierarchical structure of a namespace naming convention -- but the 2.0 IDE does not write a signature for the developer and after getting a good start developing a 2.0 application I thought I should go through the code and start marking classes and with namespaces. I keep getting the following 'missing' error even when using a first...
36
4057
by: Wilfredo Sánchez Vega | last post by:
I'm having some issues around namespace handling with XML: >>> document = xml.dom.minidom.Document() >>> element = document.createElementNS("DAV:", "href") >>> document.appendChild(element) <DOM Element: href at 0x1443e68> >>> document.toxml() '<?xml version="1.0" ?>\n<href/>' Note that the namespace wasn't emitted. If I have PyXML,
0
9734
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9607
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10653
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10395
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10137
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7674
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5564
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5700
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3876
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.