The Lurker

Latest posts | Archive

posted by ajf on 2002-09-24 at 06:23 am

OK, I understand the desire for backwards compatibility in web scripting. But look at this:

newObject = new Object( );
newObject.id = 6;
newObject.name = 'Product';
dataPointSource[ dataPointSource.length ] = newObject;

newObject = new Object( );
newObject.id = 7;
newObject.name = 'Quantity';
dataPointSource[ dataPointSource.length ] = newObject;

Here is how I might have written that:

dataPointSource.push({id: 6, name: 'Product'});
dataPointSource.push({id: 7, name: 'Quantity'});

Admittedly, the push method and the {} object literal syntax weren't introduced until Javascript 1.2 — but that code comes from a dynamic HTML example which only works in MSIE 5+ anyway.

In fact, in this example those objects are simply added to the array when the script is loaded, so it could further be simplified to:

dataPointSource = [
  // ...
  {id: 6, name: 'Product'},
  {id: 7, name: 'Quantity'},
  // ...
];

... which would have cut that array setup code from 25k to about 6k.

My point, if I really do have one, is that if your requirements restrict you to relatively recent browsers, you should take full advantage of every feature those browsers support, instead of writing unwieldy code that would work on a lesser browser when it's going to fail a few microseconds later anyway. And, when new features are introduced, get acquainted with them so you don't keep doing things the hard way.

Related topics: Web

All timestamps are Melbourne time.