Synovic.com
Demo - Dynamic Objects

For this demo I put together a simple example that demonstrates how a generic object can be defined using a different number of arguments to create table structures with a varing number of columns. By not hard coding the object signature, we can write a simple reusable method for creating a table.

This test creates a series of object passing only 2 arguments into the object's constructor.

This test uses the same object as the first, but passes the object contructor 3 arguments.

source:

    
    /*
        This function returns an array of "myObjects" that are defined
        with 2 arguments. A first name, and last name.    
    */
    function data(){
        var aData = new Array();
        aData.push(new myObject("First Name", "Last Name"));
        aData.push(new myObject("Michael", "Smith"));
        aData.push(new myObject("Mary", "Jones"));
        aData.push(new myObject("Nicholas", "Tevales"));
        aData.push(new myObject("Josephine", "Greko"));
        aData.push(new myObject("James", "Kirk"));
        return aData;
    }
    
    /*
        This function returns an array of "myObjects" that are defined
        with 3 arguments. A first name, middle name, and last name.    
    */
    function data2(){
        var aData = new Array();
        aData.push(new myObject("First Name", "Middle Name","Last Name"));
        aData.push(new myObject("Michael", "Joseph","Smith"));
        aData.push(new myObject("Mary", "Frances","Jones"));
        aData.push(new myObject("Nicholas", "Michael","Tevales"));
        aData.push(new myObject("Josephine", "Marie","Greko"));
        aData.push(new myObject("James", "Tiberious","Kirk"));
        return aData;
    }
    
    /*
        This is the generic object that will define itself based on the 
        number of arguments passed to its constructor.
    */
    function myObject(){
        var length = arguments.length;
        if (length != 0) {
            for (var x=0;x<length;x++) {
                this[x] = arguments[x];
            }
        }
        this.toString = 
            function(){
                var msg = "";
                for (var i in this) {
                    msg += "myObject." + i + " = " + this[i] + "\n";
                }
                return msg;
            }
    }
    
    /*
        This function is a object agnostic method for creating a table from
        and array of objects that store the data for a single row.
    */
    function createTable(aData, container){
        var tbl = document.createElement("TABLE");
        tbl.border="1";
    
        var tbdy = document.createElement("TBODY");
        tbl.appendChild(tbdy);
    
        for (var x=0;x<aData.length;x++) {
            var tr = document.createElement("TR");
            for (var i in aData[x]) {
                if (x == 0) {
                    var td = document.createElement("TH");
                }else{
                    var td = document.createElement("TD");
                }
                td.innerHTML = aData[x][i];
                tr.appendChild(td);
            }
            tbdy.appendChild(tr);
        }
        document.getElementById(container).appendChild(tbl);
    }