
Originally Posted by
molot0v
Only a massochist would write in this inconsistent, all but useless, slow, ungainly, inelegant language.
It’s not the language that is inconsistent, slow, ungainly and inelegant, but your code. I have checked your original code again and found the following problems:
Code:
var count = 0;
var holder = new Array(99);
function addSelectEntry() {
document.frmLib.elements.options[document.frmLib.elements.options.length] = new Option(this.label, this.id, false, false);
}
function makeNewReadingObject(label, value, description) {
holder[count] = new readingObject(label, value, description, count);
count++;
}
function readingObject(label, value, description, id) {
this.label = label;
this.id = count;
count++;
this.value = value;
this.description = description;
this.section = -1;
this.addSelectEntry;
this.getDesc;
addSelectEntry();
}
readingObject.prototype.getDesc = function ( ) {
return this.description;
}
function buttonRemoveEntry() {
box = document.frmLib.elements;
id = box.options[box.selectedIndex].value;
for ( i = 0; i < box.options.length; i++ ) {
if ( box.options[i].value == id ) {
document.frmLib.elements.options[i] = null;
}
}
}
function updateInfo() {
box = document.frmLib.elements;
id = box.options[box.selectedIndex].value;
for ( i = 0; i < box.options.length; i++ ) {
if ( box.options[i].value == id ) {
return holder[document.frmLib.elements.options[i].value].description;
}
}
return false;
}
id = This parameter is never used.
count++; = You have already incremented count in the readingObject() initialisation code. Consequently, using makeNewReadingObject() messes up the holdArray() by skipping indexes.
this.addSelectEntry; = There is no use defining this, because it goes unused.
this.getDesc; = There is no need to define this here, because you declare it with readingObject.prototype.getDesc.
. . .
Before looking back at your old code again I took the liberty of re-writing it using some better OOP techniques:
Code:
// This object defines a single readingObject.
// . . .
function readingObject ( label, value, description, id, counter ) {
this.unique_id = counter;
this.label = label;
this.value = value;
this.description = description;
this.id = id;
this.section = -1;
this.getDesc = function ( ) {
return this.description;
}
this.addSelectEntry = function ( ) {
// Commented out for testing
// document.frmLib.elements.options [ document.frmLib.elements.options.length ] = new Option ( this.label, this.id, false, false );
}
this.addSelectEntry ( );
}
// This object defines a collection of readingObject's. Use the Add()
// function to add a new object into the holdArray.
// . . .
function readingObjectCollection ( ) {
this.holdArray = new Array ( );
this.Count = function ( ) {
return this.holdArray.length;
}
this.Add = function ( label, value, description, id ) {
this.holdArray [ this.Count ( ) ] = new readingObject ( label, value, description, id, this.Count ( ) );
}
this.InspectByIndex = function ( index ) {
// Returns null if an invalid index was specified.
return ( this.Count ( ) > 0 && index >= 0 && index < this.Count ( ) ) ? this.holdArray [ index ] : null;
}
// Why not add some more complex array-inspection functions
// while we're here...
// . . .
this.InspectByLabel = function ( label ) {
for ( var i = 0 ; i < this.holdArray.length ; i ++ ) {
if ( this.holdArray [ i ].label == label ) {
return this.holdArray [ i ];
}
}
// The loop fell-through; no readingObject was found with
// the specified label, so return null.
return null;
}
}
var oROC = new readingObjectCollection ( );
// Note that I have omitted the someid* parameters. This is because
// they were never used in your original implementation of the
// readingObject.
// EDIT - You can disregard the above comment.
oROC.Add ( 'somelabel0', 'someval0', 'somedesc0', 'someid0' );
oROC.Add ( 'somelabel1', 'someval1', 'somedesc1', 'someid1' );
oROC.Add ( 'somelabel2', 'someval2', 'somedesc2', 'someid2' );
// Some tests...
alert ( oROC.InspectByIndex ( 2 ).label );
alert ( oROC.InspectByLabel ( 'somelabel1' ).id );
I tested this and it works fine. I hope you can learn something from it, and don’t be too quick to slate JavaScript in future.
More can be done with it than you could ever believe.
Bookmarks