Synovic.com
Dynamic Objects

A very cool feature of JavaScript objects is that they can be self defining.

Here is what the object looks like:

 
                //myObject is a generic object that is not pre-defined
                
                function myObject(){
                    
                    //by checking the length of the arguments sent to this object
                    //we can create a simple for loop and have the argument values 
                    //set to individual properties of the object.
                    
                    var length = arguments.length;
                    if (length != 0) {
                        for (var x=0;x<length;x++) {
                            this[x] = arguments[x];
                        }
                    }
                    
                    //override the toString  method so we can see inside the object
                    this.toString = 
                        function(){
                            var msg="";
                            for (var i in this) {
                                msg += "myObject." + i + " = " + this[i] + "\n";
                            }
                            return msg;
                        }
                        
                }
                
                //this is a simple test method that allows you to peek inside the object
                //to see how the data is stored.
                function test(){
                    var obj = new myObject("Michael", "Joseph","Smith");
                    alert(obj);
                }
                
                test();
                

So what is so fun about that? Well think about it. By taking advantage of this technique, you can create reusable generic code snipets with out hard-coding the exact arguments in the object signature.

For an example of when this would be useful see the dynamic object example. This example illustrates how the same code can be used to create a table, regardless of how many object arguments are used.