fellow web developers,
In exploring more the use of JSON in one of my projects, I ran across this
site
http://www.json.org
and particularly this script:
http://www.json.org/json2.js
I encourage everyone who does JavaScript coding to read through this script
and understand what it does, even if you have no interest in JSON. (Perhaps
it could be a little joint project for us all.)
It is a very well-designed script. I almost guarantee you will learn
something you didn't know.
One of the things I learned has settled an oddity that has bugged me for a
long time. That has to do with the construct
x = new Array()
Many of us use this constructor for both simple sequential arrays:
x = new Array()
x[0] = ...
x[1] = ...
as well as pseudo-associative arrays:
x = new Array()
x.item1 = ...
x.item2 = ...
To my surprise, when I tried to implement the json2.js method
"JSON.stringify()" I found that it failed for my pseudo-associative arrays.
I asked Douglas Crockford what was going on, and he pointed out that I
should be using
x = {}
x.item1 = ...
x.item2 = ...
instead of x = new Array(). Doing a bit of reading, I discovered to my
surprise that the use of
x.item1
has nothing to do with x being an "Array" other than than an Array is an
Object, and all Objects can be extended this way.
My point here is to recommend abandoning "new Array()" in your coding. And
instead use
x = []
and
x = {}
These are more correct, more explicit, and more informative. I suppose there
are very early versions of JavaScript (back when I was first learning the
language) that didn't allow for these, but surely those browser days are
past. (Perhaps Douglas or one of you know exactly what version that was --
anyway, I'm sure it's of no consequence.)
I have gone through three of my projects now -- The Green Chemistry
Assistant (http://fusion.stolaf.edu/gca), Jmol Protein Explorer (
http://Jmol.ProteinExplorer.org), and Jmol.js -- and they all (at least on
my local version) have no more "new Array()" constructors.
Doing this didn't take a whole lot of time and was a great exercise. I
recommend it highly.
Whether you do that or not, take a look at json2.js and learn a few new
tricks.
Thanks, Douglas.
Bob Hanson
--
Robert M. Hanson
Professor of Chemistry
St. Olaf College
Northfield, MN
http://www.stolaf.edu/people/hansonr
If nature does not answer first what we want,
it is better to take what answer we get.
-- Josiah Willard Gibbs, Lecture XXX, Monday, February 5, 1900
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Jmol-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jmol-developers