Current JSON Proposal in ES4

R

Richard Cornford

You can bring this up on the ES4 list.

Why? Are the people on the "ES4 list" sufficiently ignorant of
ECMAScript that they don't know what an Identifier is?
It would be a problem if you
could not do, say,

var unit = 'blah';

It would also be a problem if you could not do:-

unit = 'blah';

- or:-

unit;

- or:-

unit

- as they are all legal constructs with well specified behaviour in
ECMAScript (even if the last two are useless).

One way out of that is to have considerably more complex syntax rules
for - unit - which force its interpretation as a keyword in some
contexts and as an identifier in others. But that hardly lends itself to
being easily understood by programmers.

Richard.
 
E

Elegie

Richard Cornford wrote :

Hi Richard,

Indeed I well remember being surprised the first time I observed the
inline execution of a function expression back in November 2002:-

<URL:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/4eda01126dd6ea8c

(That post may include the first example of the inline execution of a
function expression ever.

Hm, no, I think that the first javascript application of an inline
execution of a function expression was actually proposed by the
stereofrog, a.k.a Gosha Bine (who many times demonstrated expert
knowledge in several programming languages, including functional
languages), whose scripts I had studied while learning the language,
months before.

The post you cite, however, may very well be the first time I used
myself an inline execution of a function expression - and I was very
happy of this "getText" function :)
I would be interested in any examples
pre-dating that one,

Here's one link, March 1st, 2002.

<URL:http://groups.google.fr/group/comp.lang.javascript/msg/53a87649e279a012>


<OT>
That brings back very good memories, actually. I was then in Stockholm
practicing finance, and found myself very interested in javascript. I
rushed on all the technical specifications I could grasp, and started
participating actively in the group.

At the time, I systematically scrutinized the answers of Jim Ley, Martin
Honnen and Gosha Bine. Gosha Bine, especially, had an incredible style;
his answers were always technically correct, and *simple* (the kind of
simplicity a true expert displays when talking about his knowledge).

I printed all the scripts he proposed on his website, then started to
try and understand the gems, always on the last night bus that would
take me home - and the driver, the same tired man every night, would
always shake his head about this young guy reading strange pieces of
paper...

I definitely owe my coding style to Gosha's scripts - except for the
simplicity, but that's certainly because I never was as expert as he was.
</OT>

Anyway, it's very nice to read you again, I hope you, and every other
'old' regulars of the group, are doing fine. Take care, and see you.


Regards,
Yep.

PS : sorry for the delay of the correction; I happened to read this post
only today (as I was researching a certain piece of code in some of my
older posts).
 
B

Brian Adkins

We are really headed towards Java, aren't we?

Hopefully not.

Thanks for the link to the spec you provided in another post. I looked
through it and found some positive things, such as "type annotations"
and "static type checking" being optional. Here are some pieces I
found interesting.

"The goals of TG1 have been to make ES4 a language that is compatible
with ES3"

"Modularity, name hiding, and library construction are supported by
packages and namespaces,"

"Generators provide a means of suspending control in a function to
return a value to the caller, and letting the caller resume the
computation later."

"A generic function is a function object that performs run-time
dispatch to one of several attached methods based on the actual types
of the argument values and the signatures of the available methods."

"ES4 has proper tail calls-the requirement that a function that calls
another function and then just returns the latter's result, or tail
calls another function and then returns nothing, must do so in a way
that does not accumulate control stack."

"[i * i for (i in naturals(10))]"

"The left-hand-side of the = operator may be a record or array
pattern,"

"Readable regular expressions"

Brian Adkins
 
D

dhtmlkitchen

If three is one running theme in your posts to this group it is that
your expectations don't match up very well with javascript.
I don't really think JavaScript was well designed. toString, for
example, should not be relied upon (OT) but ES requires it for many
methods, and even defines, in many cases, the implementation for
toString.

ES4 has a lot of changes. For example 1 global object -- a headache
saver, IMO.

Other things, like the lack of a DateFormatter, will continue to be
frustrating in those cases. Methods similar to those JR Stockton
proposed here are going into Date, though.
Wouldn't the implication of your previous statement be that if -
parseInt - was a method of the number constructor then its output would
be a Number object not a number primitive?
Agreed.

This also be justifiable placed on Integer.
We clearly have different opinions on API design. I argue that
XXX.parse should return an XXX because it seems obvious.

Yes.

An instance method of String object might"might" be expected. That's where I see the problem. it's like you
can't be certain what's coming out of it.
It is neither correct nor not correct. Rather it seems to be expedient
and harmless otherwise.
It would be incorrect on Date, right? Date.prototype.parseJSON :
Object - seems to violate principle of least astonishment.

I would think use case, side effects, and how the API is used would be
more relevant.

toSource isn't in wide use because it's proprietary, non-standard.

It's a useful feature, but the implication of changing Object's
interface to say it's serializable -- that's what I take issue w/.
That doesn't matter in context. The method exists, has existed for some
considerable time, and it has (and is having) no negative impact on the
use of objects in JavaScript(tm) or ECMAScript. Ignoring its existence
is sufficient, and any second similar serialising method can just as
effectively be ignored when not wanted with the same outcome.


As it stands javascript only has one object type, and augments that
object to create what then become its various built-in types. Then
programmers who want multiple 'types' of object set about augmenting the
native ECMAScript object into the various 'types' they want. In the end
there is still not actual distinction between the 'type' of these
objects beyond what has been done to them and how they will be used. And
that distinction comes from the programmer not the language.

If the programmer declares that 'these objects are data structures' and
'these objects are not' then that is his/her design decisions, but the
objects themselves are still all just the native ECMAScript object in
reality. Thus, even if all the objects have serialisation method (and in
JavaScript(tm) they already do) then the distinction between a 'data
structure' and 'not a data structure' may be no more than calling the
serialisation methods of the first and never calling it on the latter.

This is the reality of tiring to do OO programming in a language as
loosely typed as javascript. There is no 'type safety' and the
programmer is the one responsible for staying on top of that and
understanding how the notions of 'type' from their design are to be
used/handled in the resulting program.






Because the addition you are talking about makes no practical
difference. It does not introduce anything new (object serialisation has
been a reality for a considerable time already) and so its only
contribution to the ability of 'programmers' to do stupid things is
providing another Identifier that can be put after a dot and before an
arguments list without the result actually erroring on the spot.
Cross browser?
Then don't call it. Your tool tip manages to get by without you
calling - toSource - on it.
Well, clients might call it, and would be perfectly correct in doing
so. When Tooltip 2.0 comes out, then toJSONString -- a contract that
Tooltip was forced into -- has different results (which the Tooltip
author may have neglected to consider). Maybe the client sees the
difference right away, or maybe the cleint's app seems to be working
for a while and then they realize that the serialization is different,
and includes an x property but the _id property is different.

toSource doesn't describe the object's public interface except in a
minority of browsers.
These are examples of "misuses" of "for-in loops, or closures"?

Closures. I've got some explanation below of why it's confusing.
"Confusing code" can often be a matter of experience and knowledge. As a
relative novice you will tend to be confused by many of the more
advanced constructs used in javascript, and perceive some of those
constructs as strange. That is inevitably true for anyone coming anew to
any programming language.

It is also possible for code that is inherently strange and confusing
(to any audience) to be written in any programming language.

JavaScript seems to make confusing, strange code almost necessary.
It's more condusive to weird constructs (like my weird singleton you
mention below). It makes it awkward to do things that should be easy
and straightforward -- like private.
What is needed with of your response to my request for examples of
"misuses of for-in loop, or closures" is some evidence for "the Power
Constructor" and "the Module pattern" being such "misuses". Or even of
their being inherently "strange and confusing". Otherwise we are left
with your unsubstantiated assertions, and may be included to attribute
them as the result of ignorance on your part rather than being anything
of significance.
OK.

Developers on my last gig were seriously confused by the module
pattern and would think that it was a namespace or part of the
namespace. I was there till 8 or later many nights. 10 hour days
fixing this stuff, so yeah, pretty annoying. All this in an anti-XP
environment, developing a heavy, feature-rich, internal app, with
waterfall process.

YAHOO.namespace("foo.bar");

// WRONG: namespace converted to object.
YAHOO.foo.bar = (function(){

if(YAHOO.foo.bar.hasGlobal) {

}
YAHOO.foo.bar.
function baz(x){ return x; }; // Function with hasGlobal for
[[Scope]]
// 1100 lines or so snipped..
return {
hasGlobal : false,
// baz method which points to baz (3)
baz : function(x) { return YAHOO.foo.bar.baz(x): }// Function 3
with hasGlobal for [[Scope]]
}
};

Now if there had been better teamwork and communication (and a smarter
mgr), they could have realized that the module pattern was causing
problems the way they were trying to use it. That started to happen
after I got there (more communication, pairing up, daily standups, et
c).

I think my boss told me to "change as little as possible" and "don't
break anything." Very hard to work with this code and boss, which was
very non-modular.

It took quite a long time to convince these guys to break stuff up
into multiple files -- about 5 months. We ended up with 25-30 files/
page. Convincing them not to use the memory-intensive module pattern
was more difficult, though they did finally see the mistake in the way
they were using it previously. They had just grown accustomed to it.

Doug Crockford seems to be taking credit for this pattern. He's never
once mentioned Richard Cornford.
Take "the module pattern" (as I am largely responsible for its
existence); about 6 months into to the development of the module pattern
the question of singleton pattern implementations came up, and it
rapidly became obvious that the module pattern lent itself extremely
will to that particular application. So much so that most introductions
to the module pattern have employed a singleton pattern as their primary
demonstration of the application of the module pattern (including the
well-known YUI article, where the name got attached to the technique).
The guys at Yahoo use this pattern and got really confused by it, I've
gone into detail w/this on the ES4 list, and above. Anyway, this is a
little OT.
On your web site you have a section headed "Design Patterns" which seems
to contain a single article that is a singleton pattern implantation:-

<URL:http://www.dhtmlkitchen.com/?tq=Design Patterns>
I like patterns, and the mindset taht goest into them (encapsulate the
parts that vary). I've used Factory, pub/sub, Command, and even State.
Haven't wrote them all out for JS, though.
- which I find strange, and many novices would certainly find confusing.
One of the main things about your code that I find strange is its
ham-fisted attempt to achieve a notion of 'private' for an exposed
'class' constructor. Having a constructor for an object that you only
intend to ever have one instance of is itself a strange design decision
in javascript, as an object literal is an ideal structure for defining
an object that will only ever be a single unique object instance. But
the really strange thing in your code is that it employs the
JavaScritp(tm) extension - Array.prototype.indexOf - method in the code
that is supposed to very that the 'class' constructor is not directly
used by external code, and so will not work at all with any JScript
versions. That is strange because any 'Design Pattern' that cannot be
used with IE browsers has virtually no commercial applications at all
and so is virtually worthless.

Another strange aspect of your implementation of "private" for the
'class' constructor is how trivial it is to subvert. All a programmer
has to do in order to use the singleton 'class' constructor directly is
first execute:-

Function.entryCheck = function(){return;};
Trivial to subvert, true, I actually sometimes forget in MY OWN CODE,
and reinstantiate when I really wanted JCO (just create one)_. that's
why I like the throw Error() over having a problem when you've got two
objects and the program expects no more than 1.

I'm really not satisfied with my "singleton" approach. A runtime error
is bad, and the other points you mention (confusing to others), the
constructor is accessible (not private), but has a doc comment that
says "private" (contradictory). The access modifiers in ES4 are really
what I want.

One more thing I should mention is that Thomas mentioned that there is
no Map in ES4. This is a false statement. Hopefully others won't be
confused by it. Just wanted to clarify. ES4 has Map<T,T>. The <T,T> is
parameterized, so can be var m:Map<String, Widget> = new Map(...)

Finally, I'm really glad to see that the TG1 group has decided that
Object.prototype.toJSONString is out, and have removed it from the ES4
spec.

Garrett
 
T

Thomas 'PointedEars' Lahn

I don't really think JavaScript was well designed. toString, for
example, should not be relied upon (OT) but ES requires it for many
methods, and even defines, in many cases, the implementation for
toString.

What are you talking about? ES3 does not specify algorithms that depend or
rely upon ToString() return values. It defines the implementation for
ToString() and for the toString() method of built-in objects, and rightly so.

That does not mean the toString() method could not be overwritten; it can,
and the resulting property appears to retain its DontEnum attribute (tested
with Firebug only):

function strRepeat(s, n)
{
if (n < 0) n = 0;

var a = [];

while (n--) a.push(s);

return a.join("");
}

Object.prototype.toString = function(level)
{
if (arguments.length < 1) level = 0;

var s = strRepeat(" ", level);
var a = [s + "{"];

for (var p in this)
{
a.push([
" ", s, p, ": ", this[p] && this[p].toString(level + 1)
].join(""));
}

a.push(s + "}");

return a.join("\n");
};

window.alert({foo: {bar: "baz"}});
ES4 has a lot of changes. For example 1 global object -- a headache
saver, IMO.

What do you mean? There is already only one Global Object since ES1,
and it can be referred to in the global execution context with `this'.

Please trim your quotes.
Finally, I'm really glad to see that the TG1 group has decided that
Object.prototype.toJSONString is out,

I'm not. It would have been a useful addition. Now we will have to
continue to include a larger and considerably slower library for that feature.
and have removed it from the ES4 spec.

Too bad.


PointedEars
 
D

dhtmlkitchen

What are you talking about? ES3 does not specify algorithms that depend or
rely upon ToString() return values. It defines the implementation for
ToString() and for the toString() method of built-in objects, and rightly so.

Date.prototype.toString is defined to return something parseable by
parse()

Number.prototype.toString( radix ) -- also defined by the spec.

The API can't change these without breaking existing code.
That does not mean the toString() method could not be overwritten; it can,
and the resulting property appears to retain its DontEnum attribute (tested
with Firebug only):
That is true; a property's attributes are not changed when it's value
is replaced. Even JScript gets this right.

What do you mean? There is already only one Global Object since ES1,
and it can be referred to in the global execution context with `this'.

if(top != self) {
// different global object.
}

The headaches come from trying to, for example, see if an object is an
Array.

function isArray( it ) {

}

The frames/windows issue is the part that makes it hard.
 
T

Thomas 'PointedEars' Lahn

Date.prototype.toString is defined to return something parseable by
parse()

That is a definition in the opposite direction than you implied.
Number.prototype.toString( radix ) -- also defined by the spec.

Again I don't see a definition that would rely on the return value of this
method.
The API can't change these without breaking existing code.

"The API", whatever you understand by that, does not need to change these.
if(top != self) {
// different global object.
}

You are confusing the language and one of its possible runtime environments.
`top' and `self' are properties of a Window host object as provided by the
AOM. It makes sense that every global execution context has its own Global
Object, and I doubt ES4 is going to change that as that would make it
incompatible with current Web applications.
The headaches come from trying to, for example, see if an object is an
Array.

function isArray( it ) {

}

You are not making any sense.
The frames/windows issue is the part that makes it hard.

Which does not have anything to do with the programming language.


PointedEars
 
R

Richard Cornford

On Oct 27, 2:28 pm, Richard Cornford wrote:
Doug Crockford seems to be taking credit for this pattern.

I have never seen any evidence of his doing that. And I would be very
surprised if he did, mostly because I have always regarded him as having
too much intellectual integrity, but also because I know he is smart
enough to know that the historical record would not support such a
claim.

Other people may be crediting him with the invention, but those people
also seem to regard it as something new that can be subject to ongoing
development rather than being an idea that is nearly half a decade old
and has already seen the extensive examination of its possible
permutations and applications.

Still, just so we don't have to waste time disputing this here is the
history:-

It starts with a question about creating static members of 'classes' in
javascript:-

<URL: http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/dea2a95df1e355ca >

Both Douglass Crockford and I answered that question, and we both gave
the 'accepted wisdom' answer of the time; that static members were
created as public properties of constructor for the 'class'. You will
notice that Douglass Crockford does not mention any possibility for
private static members, nor does he hint at any possibilities in that
direction.

Laurent Bugnion responded to one of my posts saying that as far as
he knew there was no method of creating private static 'class' members
(Laurent Bugnion being one of the individuals who would have been
expected to know of a method if a method had been known at the time).
And that got me thinking, because I knew about Douglass Crockford's
method for creating private instance members and just felt that there
must be some way of doing a similar thing at the constructor/'class'
level to create private static members (or at least emulate them in
a way that was sufficient for all practical purposes). Fortunately
(from my point of view) at the time I was getting interested in the
functional programming aspects of javascript and had been exposed
to, and surprised by, the inline execution of functions expression
in examples such as:-

<URL: http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/4eda01126dd6ea8c >

So with that concept sitting in the back of my mind, after a few hours
struggling to find some devious means to apply Douglass Crockford's
method for creating private instance members at the 'class' level the
penny eventually dropped and wrapped the 'class' constructor in the
inline execution of a function expression and had a private scope at the
class level, and so somewhere to put 'private static' members. So I
posted that (7.48 pm GMT April 23rd 2003).

Using the inline execution of a function expression to create a 'private
scope' is the essence of the module pattern, and it rapidly became
apparent to me that it had more immediately practical applications than
emulating private static members of 'classes' (especially given that
javascript authors had been, at the time, getting by quite happily
without private static members for the previous 8 years). The direction
I immediately took was to create self contained stand-alone modules
wrapped in the inline execution of function expressions, and these are
the 'module pattern', the first examples of which were published in the
(historically extremely significant) "closures, what are they good for?"
thread (in my May 8th post (that is May 9th local time (BST))):-

<URL: http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/793cd655ad56fe91 >

The following two years witnessed the publishing of a steadily
increasing number of 'module pattern' implementations, along with the
examination and development of the ideas behind it. For example, the
'singleton' implementation that is the subject of the YUI blog page on
the 'module pattern' first appeared in August 2003:-

<URL: http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/caa52610080c9de3 >

The bulk of the work in the field was done by Yann-Erwan Perio (Yep),
Lasse Reichstein Nielsen, Michael Winter and myself. Indeed I have no
recollection of Douglass Crockford ever even commenting on the idea,
though he obviously was paying attention.
He's never once mentioned Richard Cornford.

Mentioned me to who? If nobody asked the question then they would never
have heard the answer.

(I will answer the rest of your post tomorrow, or later in the week.)

Richard.
 
D

Dr J R Stockton

In comp.lang.javascript message <72041073-fe46-4478-97fb-e06e9081fbf3@i2
9g2000prf.googlegroups.com>, Sun, 25 Nov 2007 14:40:45,
Other things, like the lack of a DateFormatter, will continue to be
frustrating in those cases. Methods similar to those JR Stockton
proposed here are going into Date, though.

But will they be sufficient, well-designed, and error-free? Will, by
using them, users be able to comply with all relevant international
standards, ISO and other, without additional effort?
 
R

Richard Cornford

I don't really think JavaScript was well designed.

Your point being?
toString, for example, should not be relied upon (OT) but ES
requires it for many methods,
What?

and even defines, in many cases, the implementation for
toString.

ES4 has a lot of changes. For example 1 global object -- a
headache saver, IMO.

If they have no intention of maintaining back-compatibility with ES3 I wish they would just come
out and say so.
Other things, like the lack of a DateFormatter, will continue
to be frustrating in those cases.

You mean in cases where someone needs a date formatter but does not know how to write or find
one for themselves?
Methods similar to those JR Stockton
proposed here are going into Date, though.

Agreed.

This also be justifiable placed on Integer.


We clearly have different opinions on API design. I argue that
XXX.parse should return an XXX because it seems obvious.


Yes.

An instance method of String object might
"might" be expected. That's where I see the problem. it's
like you can't be certain what's coming out of it.

You can be certain as soon as there is a specification stating what will be coming out of it.
It would be incorrect on Date, right?

And is that being proposed?
Date.prototype.parseJSON : Object - seems to violate
principle of least astonishment.

So why is that relevant to whether string objects should have such a method?

I would think use case, side effects, and how the API is
used would be more relevant.

toSource isn't in wide use because it's proprietary, non-standard.

Yes, but it still demonstrated that having the method is not of itself harmful or problematic.
It's a useful feature,

No, it is not that.
but the implication of changing Object's
interface to say it's serializable -- that's what
I take issue w/.

But that is not a change when the objects are already serializable. And in implementations where
it was a change the outcome would be no more problematic than they have been in JavaScript(tm).
Nobody is going to be forced to use a method just because it happens to exist.

<snip - you really should do something about learning how to trim your quotes. It is not that
difficult,>
Cross browser?

Your point being?
Well, clients might call it,

Clients? What are you worrying about here? That code that works OK if used as instructed might
not do anything sensible if exposed to someone who randomly types method calls on its objects
without understanding what they are doing? Well yes, garbage in - garbage out. We might worry
about the those same individuals being in possession of pens, pencils and other pointed objects
in case they put their own eyes out.
and would be perfectly correct in doing so.

They would not be correct in doing so unless they understood what the outcome of their actions
was going to be, at which point whatever the outcome was it would be both expected and desirable
in context.
When Tooltip 2.0 comes out, then toJSONString -- a
contract that Tooltip was forced into -- has different
results (which the Tooltip author may have neglected
to consider).

You have been saying that it makes no sense to serialize Tooltip at all, so when Tooltip 2.0
comes out it still makes no sense to serialize it.
Maybe the client sees the difference right away, or
maybe the cleint's app seems to be working for a while
and then they realize that the serialization is different,
and includes an x property but the _id property is different.

toSource doesn't describe the object's public interface except
in a minority of browsers.


Closures.

You are saying the module pattern is an example of a misuse of closures?
I've got some explanation below of why it's confusing.

So still not explanation of why it is a "misuse"?
JavaScript seems to make confusing, strange code almost necessary.

But what is confusing to a novice is still not necessarily inherently confusing. Javascript
itself is an incredibly simple language; it is after all defined in just 170 pages of text.
It's more condusive to weird constructs (like my weird
singleton you mention below).

The module pattern singleton is not "weird". It is a simple and fixed structure of objects, and
when understood as such it is really quite obvious.
It makes it awkward to do things that should be easy
and straightforward -- like private.

How could "like private" (whatever that is supposed to mean) ever be that straight forward in a
language that has no notion of 'private'? We are talking about implementing a non-native
concept, and you do that by programming that concept in the language you are using.
OK.

Developers on my last gig were seriously confused by the
module pattern and would think that it was a namespace or
part of the namespace.

So they were trying to use closures without first understanding closures and their mechanism?
Aggravated by someone throwing one of those stupid 'namespace' constructs into the mix?
I was there till 8 or later many nights. 10 hour days
fixing this stuff, so yeah, pretty annoying.

Fixing the products of individuals who where never qualified to do the jobs they were attempting
is usually annoying.
All this in an anti-XP environment, developing a heavy,
feature-rich, internal app, with waterfall process.

YAHOO.namespace("foo.bar");

// WRONG: namespace converted to object.
YAHOO.foo.bar = (function(){

if(YAHOO.foo.bar.hasGlobal) {

What is this you are posting? Is it real code or something you made off the top of your head for
some reason? Are we really dealing with developers so inept as to be thinking that 'foo' and
'bar' were sensible names for namespaces?
}
YAHOO.foo.bar.
function baz(x){ return x; }; // Function with hasGlobal for
[[Scope]]
// 1100 lines or so snipped..
return {
hasGlobal : false,
// baz method which points to baz (3)
baz : function(x) { return YAHOO.foo.bar.baz(x): }
^^^^^^^^^^^^^^^^^
Too much recursion, if this method really is going to call itself.
// Function 3 with hasGlobal for [[Scope]]

Are these nonsense comments original or your doing? They demonstrate someone who does not have a
clue what they are doing.
}
};

Now if there had been better teamwork and communication
(and a smarter mgr), they could have realized that the module
pattern was causing problems

What you have shown here is not the module pattern causing problems; it is inept developers
attempting to do something they are not qualified to do causing problems. That probably was a
fault of management and/or communication and/or teamwork, though it could also have been
unrealistic overconfidence on the part of the developers responsible stopping them from being
able to recognise their own shortcomings (a common symptom in people in the early stages of
learning javascript).
the way they were trying to use it.

You see, not the module pattern but rather the result of the programmers trying to use it
without understanding it.
That started to happen after I got there (more communication,
pairing up, daily standups, etc).

So you have seen how people with unfounded overconfidence need to be confronted with their
failing before they can go on to learn to do things properly.
I think my boss told me to "change as little as possible" and
"don't break anything."

If the code you posted actually is representative then you never had the option of breaking
anything, as it never worked to start with.
Very hard to work with this code and
boss, which was very non-modular.

It took quite a long time to convince these guys to break
stuff up into multiple files -- about 5 months. We ended up
with 25-30 files/page. Convincing them not to use the
memory-intensive module pattern was more difficult,

The module pattern is not significantly memory intensive.
though they did finally see the mistake in the way
they were using it previously. They had just grown
accustomed to it.

If those comments in the code you posted are genuine then I don't see how what they were doing
could be described as becoming accustomed. they clearly didn't have a clue what they were doing.

I like patterns, and the mindset taht goest into them
(encapsulate the parts that vary). I've used Factory, pub/sub,
Command, and even State. Haven't wrote them all out for JS,
though.

It might be an idea to put some more time into learning javascript before you do because that
first effort was rubbish.

Trivial to subvert, true, I actually sometimes forget in MY
OWN CODE, and reinstantiate when I really wanted JCO (just
create one)_. that's why I like the throw Error() over having
a problem when you've got two objects and the program expects
no more than 1.

Which is where never exposing the constructor comes into its own. You don't have to throw errors
or remember what you should be doing because you just cannot create that second object.
I'm really not satisfied with my "singleton" approach.

Good, because it really is very bad, indeed the second worst I have ever seen.
A runtime error is bad, and the other points you mention
(confusing to others), the constructor is accessible (not
private), but has a doc comment that says "private"
(contradictory). The access modifiers in ES4 are really
what I want.
<snip>

Fine so write it in ES4 code. In the real world we will be doing it with ES3 code for some years
to come so it is much more important to be able to do things well with what we have got. You
notice that the ES3 module patern version already has everything lacking in your version, and
has been around for long enouth for everyone to be pretty confidnete that it is reliable.

Richard.
 
D

Dr J R Stockton

In comp.lang.javascript message <55582ec7-ee15-4cdb-a028-3340b0c8c153@s8
g2000prg.googlegroups.com>, Sun, 25 Nov 2007 17:23:18,
On Nov 25, 3:31 pm, Thomas 'PointedEars' Lahn <[email protected]>
wrote:

Date.prototype.toString is defined to return something parseable by
parse()

To me, it looks more the other way round; Date.parse is defined as
accepting anything that Date.toString() can generate.

But I see nothing to prohibit Date.toString() returning, for today,
2007-W48-1 or 2007-330. The former is more useful than 2007-11-26,
since it clearly indicates not only the time-of-year but also the day-
of-week, both of which have far more practical effect than day-of-month.
 
D

dhtmlkitchen

You mean in cases where someone needs a date formatter but does not know how to write or find
one for themselves?
Writing a DateFormatter correctly, considering L10N, is no simple
task.
You can be certain as soon as there is a specification stating what will be coming out of it.
Docs can be boring and time consuming (for many); A sensible API with
obvious method signatures, on the other hand, does not prevent someone
from delving into the details of API docs.

And is that being proposed?


So why is that relevant to whether string objects should have such a method?
If String.prototype, why not Date.prototype? or why String (orig)?
<snip>



Yes, but it still demonstrated that having the method is not of itself harmful or problematic.


No, it is not that.



Your point being?
toSource is not really practical for use in the mainstream; noone
realistically expects it to work cross browser, don't you agree?

Clients? What are you worrying about here? That code that works OK if used as instructed might
not do anything sensible if exposed to someone who randomly types method calls on its objects
without understanding what they are doing? Well yes, garbage in - garbage out. We might worry
about the those same individuals being in possession of pens, pencils and other pointed objects
in case they put their own eyes out.

toJSONString was proposed to be part of any object's public interface.

A menu, datagrid, sortable list, et c [insert-your-fav-widget]
implementations might really want serialization. Even if their API
didn't consider it!

The API designer might not be considering serialization, or might have
partially considered it (worse) but the toJSONString hook has already
been added; it's a part of the API. This part of the API might,
coincidentally, work for the user.
They would not be correct in doing so unless they understood what the outcome of their actions
was going to be, at which point whatever the outcome was it would be both expected and desirable
in context.


You have been saying that it makes no sense to serialize Tooltip at all, so when Tooltip 2.0
comes out it still makes no sense to serialize it.
Serialization might make sense for one user of Tooltip.

But the Tooltip API developer doesn't want to concern himself with
serialization; it makes Tooltip less cohesive. When he changes his
code for 2.0, the code still works great.

But someone else was relying on the serialization of toJSONString, so
their code breaks.
So they were trying to use closures without first understanding closures and their mechanism?
Aggravated by someone throwing one of those stupid 'namespace' constructs into the mix?
That pretty much sums it up. I'm not going to debate the namespace
issue now. Save that one for another topic.

Fixing the products of individuals who where never qualified to do the jobs they were attempting
is usually annoying.
Especially when refactoring is almost banned.

What is this you are posting? Is it real code or something you made off the top of your head for
some reason? Are we really dealing with developers so inept as to be thinking that 'foo' and
'bar' were sensible names for namespaces?
Off the top of my head but based on real code. I don't have it on
file, unfortunately. I've edited the code snip below.

The F/E programmers weren't great; the project recs were not simple,
we were using YUI, and other developers were influenced by practices
they had applied individually (not collaboratively).
}
YAHOO.foo.bar.
function baz(x){ return x; }; // Function with hasGlobal for
[[Scope]]
// 1100 lines or so snipped..
return {
hasGlobal : false,
// baz method which points to baz (3)
baz : function(x) { return /*YAHOO.foo.bar.*./ baz(x): }

^^^^^^^^^^^^^^^^^
(code snippet edited).
<snip>
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top