Re: Current JSON Proposal in ES4
- From: "dhtmlkitchen@xxxxxxxxx" <dhtmlkitchen@xxxxxxxxx>
- Date: Tue, 23 Oct 2007 02:08:19 -0000
On Oct 22, 4:01 pm, "Richard Cornford" <Rich...@xxxxxxxxxxxxxxxxxxx>
wrote:
dhtmlkitc...@xxxxxxxxx wrote:
On Oct 22, 12:33 am, Richard Cornford wrote:
dhtmlkitc...@xxxxxxxxx wrote:
<snip>
The example was taken from Java, to illustrate the concept thatInteger.parseInt <-- what should this return?
No such construct exists in javascript as there is no Integer object
and - parseInt - is a global function.
XXX.parse is usually expected to return an XXX.
parseInt should be a Number function, not global.parseInt.
Number.parseInt
BTW, global is accessible in ES4.
Like a date string? A Number? How about...Date.parse <-- what should this return?
A 'static' method of the Date constructor might be expected to return
something that was date related.
A Date ?
Ding, ding, ding!
Why is adding the parse method to String correct?now,
String.parseJSON <-- should return what, a String?
Wasn't it - String.prototype.parseJSON - that you were objecting to? An
instance method of String object might be executed to turn that string
into anything. As JSON is a structure consisting of and object or array
with optional nested descendants then that is the likely result.
Should the String constructor be morphed into a parser?
JavaScript -- not ES. It's a really old feature, and only Moz/NS4.The current proposal, Object.prototype.toJSONString,
complicates objects with responsibility that they
should not necessarily have.
Why shouldn't an object have a specialised serialisation
method?
Why should it?
Precedence would be a good enough reason, as JavaScript(tm) already
has - toSource - method on objects. All this adds is another form of
serialisation, and one that has obvious practical applications.
While the feature may have practical applications, such applications
are quite limited. Limited to what? Well, mostly to Data Structure.
Object should not always have data structure functionality. Only data
structures should. A Menu, could, for example, be converted to a Data
Structure, or could be so implemented. This would be a design
decision.
toSource is limited to Spidermonkey, AFAIK.If it needs one, it can get it independently.
Maybe, but not a native code (so relatively fast) method.
It could define it's own
method for serialization. Not all objects need this.
Much as objects don't need - toSource - but don't suffer for having it.
toJSONString will be an ES4 standard.
Exactly! JavaScript lets you do all sorts of things.It will be easy to misuse.
An example of this "misuse"?
HTMLInputElement.prototype.toJSONString = getPWD;
There is plenty of scope for doing stupid things with the language as it
is.
"foo".prop
new Script("true");
"foo".big();
So why add another? And besides, toJSONString would be really useful
in a lot of cases. I realize that, really. But, at the same time, I
don't need it on my Tooltip.
Like the Power Constructor, the Module pattern. These things can andAny of the misuses of for-in loop, or closures
would seem not as bad.
Which misuses?
are often used to make really strange and confusing code. There are
plenty of F/E devs who struggle trying to apply OO concepts to JS and
make judgement calls that create problems down the road.
It will conflate Object.prototype with implementation
details possibly leaking into places they shouldn't.
Instead of keeping reduced access, it maximizes access.
It does not allow the functionality to be tested and
debugged independently.
If these features go in the language, implementations and
library authors will be required to handle this method
for all objects, of any type, forever. The change will
be permanent and unretractable.
This feature is not critical; it can be added at any time.
The flip side to that is that once added, it cannot be
removed. ever.
There are many assertions here but not one substantial
argument to back any of them up. Between ECMA 262 2nd
edition and 3rd edition the object prototype gained
hasOwnProperty, propertyIsEnumerable and isPrototypeOf
methods with no negative impact. Adding methods to the
object prototype does not necessarily have any impact
on anything else at all.
propertyIsEnumerable attempts, but fails in addressing the
problem of determining if a property is enumerable. It is
broken, as defined by ECMA-262.
You have said that before, but still have not posted any demonstration
of this supposed brokenness.
function b() {}
b.prototype.pie = "yes"; // enumerable prop.
var i = new b;
i.propertyIsEnumerable('pie'); // Well, what do you think?
for(var prop in i)
alert(prop); // will it alert "pie"?
What went wrong?
propertyIsEnumerable does not check the prototype chain, but for-in
does.
That is a bug.
Yes, well, *mostly* the bug itelf is incosistent.The JScript bug eliminates any possibility of this method
being suitable for cross-browser use.
Which JScript bug? The one where - dontEnum - attributes are inherited
through the prototype chain when they should not be?
({prototype:1}).propertyIsEnumerable('prototype'); // well, what does
JScript think?
(Function()).propertyIsEnumerable('prototype'); // well, what does
JScript think?
All JScript bugs aside, Object is a generic type. HashMap, orObjects need to deal with enumeration;
they're stuck with this
responsibility. This is is a problem.
It is not a problem. It has never been feasible to directly use
javascript objects as storage faculties for mapping arbitrary string
keys to values. The 'safe' set of possible keys in such a context don't
suffer from enumeration issues even in IE browsers.
Map<String,Object>, is specific. It says: I'm a data structure.
Well, instanceof was there first. In ES4, it will work across frames.isPrototypeOf is useless/harmless. Can't be removed,
though.
As useless/irrelevant as - instanceof -.
It will be used. A lot, I think.toJSONString would not be so benign.
Why not, it would do no harm if not used, and if used then there would
presumably be some reason for using it.
It's almost as bad is Object enumeration.
What is "bad" about object enumeration?
Enumeration has nothing to do with Object as a type. The feature
causes problems that have been the topic of discussion for years.
I've had my share of problems with this. Mostly related to the JScript
Or maybe worse. It means that every object is, by
default, not just a data structure, but a data structure that
also supports a specialized (or default) serialization.
Which is already the reality for JavaScript(tm) and that doesn't seem to
have resulted in any problems.
bug.
JSON is not the panacea, for those who think it is, a native JSONHow in any way, is it justified?
Expedience, in the face of a growing use of JSON as a data exchange
medium, and particularly for transmission between HTTP clients and
servers.
object will provide the same functionality, and will do nothing other
than provide such functionality.
Besides being misnamed propertyIsEnumerable is broken.Maybe 2% of all objects will need serialization at most.
Maybe, maybe not. What of it, I don't thing I have ever used
propertyIsEnumerable in production code but no objects have suffered for
having that unused method?
Because it means that by default, Widget MUST tackle thisfor example:
x.toJSONString();
if x is an instance of
function Widget() {
_name : "widget_" + widget.instances.length;
}
Well, then you have to filter out the _name property in your
serialize method.
Why? What is it doing there if you don't want it included in the
serialisation, or if you don't want it why use the prototype
toJSONString method? You are the one doing the programming so program
the system to be what you want it to be.
responsibility. Widget is Serializable, even if I don't want it to be,
I, as a programmer, have had that right taken away. That sucks.
A Tooltip.show() method seems pretty rational to me. What does thatAlternatives
1. A built in JSON object
Because JSON translates directly into structures of javascript
objects/arrays it makes no sense to attempt to create a specific
JSON object.
JSON is a subset of JavaScript
Yes, a restriction of the nature of property name declarations in the
object literal strings and the types of values that may be assigned to
properties.
JSON translates into JS structures.
Which is what we are doing, taking a JSON string and getting a
javascript object.
JS Structures do not translate to JSON.
They can (or the whole thing would be a non-starter). All you are saying
is that objects can be created that will not translate well. And they
can, but that does not mean that rational programmers will create such
objects.
have to do with data structures?
Exactly. The serialization has to, and if it doesn't the programmer
It is not justified in ES3.
Adding serialization capabilities to every object creates
unnecessary complications. Each type of object has to deal
with this.
More like the serialisation process has to be able to deal with (in some
predetermined manner) all possible javascript objects. But that is just
a mater of specification.
has to provide it.
I can see it now, people will want next a way to set a serializable
flag on their properties to make things easier.
Not required, but they do, in many browsers. In those cases, it willHow would you serialize a Callable, a subclassed string,
an HTMLInputElement?
HTMLInputElement are not required to have prototype chins or inherit
form Object.protoype so they are irrelevant in the general case. Other
objects have properties with attributes and values regardless of their
'type'.
be required to deal with serialization.
And Callable is an Object, and a subc'd String is a String.
How is adding JSON methods to all objects justified over adding aHow is any of the above justified?
separate Class/Object to handle serialization?
<snip>Not harmless when my Tooltip has to be responsible for serializing
How is any of what justified? The proposed methods are an expedient or
harmless addition to the group of methods already on the pertinent
prototypes.
itself.
Before having a JSON class implemented, I'd like to see three basic
types supported by a JSON object (like Map, et c).
a JSON Object would do:
serialize
deserialize
For those interested, Robert Sayre is implementing some JSON object
support in Mozilla, something along the lines of Caja.
Garrett
Richard.
.
- Follow-Ups:
- Re: Current JSON Proposal in ES4
- From: Richard Cornford
- Re: Current JSON Proposal in ES4
- From: Peter Michaux
- Re: Current JSON Proposal in ES4
- References:
- Current JSON Proposal in ES4
- From: dhtmlkitchen@xxxxxxxxx
- Re: Current JSON Proposal in ES4
- From: Richard Cornford
- Re: Current JSON Proposal in ES4
- From: dhtmlkitchen@xxxxxxxxx
- Re: Current JSON Proposal in ES4
- From: Richard Cornford
- Current JSON Proposal in ES4
- Prev by Date: Elementary question I suppose, using the parameters.
- Next by Date: Re: Current JSON Proposal in ES4
- Previous by thread: Re: Current JSON Proposal in ES4
- Next by thread: Re: Current JSON Proposal in ES4
- Index(es):
Relevant Pages
|