Mozilla.com

  1. MDC
  2. Main Page
  3. Sandbox Test Foo
  4. Javascript Object Literals

JavaScript has become the best friend of a new legion of hackers that want to take advantage of the new paradigms in web development.

In this article we will explore the basic data structures we can create and exchange with a server using JavaScript object literals and AJAX.

Basic syntax:
   anyvar = "value";
   array  = [item,item,item];
   list   = {0:value, 1:value};
   map    = {key:value, key:value};
   object = {key:value, key:[array...], key:{map...}};
   table  = [[array...], [array...]];
   fault  = {error:123, text:'Something very bad happened'};

The easiest way to exchange JavaScript objects is to generate a stream on the server in the following PHP example:

<?php
header('Content-type: text/plain');
echo "person={name:'Britney', age:22, active:true}";
?>

then eval() the responseText from an XMLHttpRequest call in the client assigning it to an object for later use:

   var data = eval(responseText);
   alert(data.person.name);
Primitives: string, number, bool, null
   var1 = "britney";
   var2 = 123;
   var3 = true;
   var4 = null;
   write(var1);
Array or list
   var1 = [111,222,333];       // homogeneous
   var2 = ['britney',22,true]; // heterogeneous
   write(var1[0]);
   write(var2[0]);
An array is a map with numeric keys
   var1 = {0:111, 1:222, 2:333};       // homogeneous
   var2 = {0:'britney', 1:22, 2:true}; // heterogeneous
   write(var1[0]);
   write(var2[0]);
Map/object/hash/struct: an array with named keys
   var1 = {name:'britney', age:22, active:true};
   write(var1['name']);
   write(var1.name);
Complex object: mixed structures
   person = {
       name:'britney', 
       age:22, 
       address:{
           city:'Sunny', 
           state:'FL'
       },
       phones:['555-1234','555-1235']
   };
   write(person.name);
   write(person.address.city);
   write(person.phones[0]);
Table: a 2D array
   data = [
       ['britney', 22, true , '555-1234'],
       ['avril'  , 19, false, '555-4321'],
       ['christy', 21, true , '555-9876']
   ];
   write(data[0][0]);
Table: a list of arrays
   data = {
       0:['britney', 22, true , '555-1234'],
       1:['avril'  , 19, false, '555-4321'],
       2:['christy', 21, true , '555-9876']
   };
   write(data[0][0]);
Mapped Table: a list of structs
   data = {
       0:{name:'britney', age:22, active:true},
       1:{name:'avril'  , age:19, active:false},
       2:{name:'christy', age:21, active:true}
   };
   write(data[0]['name']);
   write(data[0].name);

or

   row = data[0];
   write(row.name);
Typed Table: a table with header structure
   data = {
       colcount:3,
       rowcount:3,
       cols:[
           ['name'  , 'string' , 40, 0, false],
           ['age'   , 'integer',  8, 0, true ],
           ['active', 'boolean',  1, 0, true ]
       ],
       rows:[
           ['britney', 22, true ],
           ['avril'  , 19, false],
           ['christy', 21, true ]
       ]
   };
   write(data.colcount);
   write(data.cols[0][0]);
   write(data.rows[0][0]);
Datasets: a group of tables
   dataset = {
       tables:{
           customers:{
               cols:[ /*here*/ ],
               rows:[ /*here*/ ]
           },
           orders:{
               cols:[ /*here*/ ],
               rows:[ /*here*/ ]
           }
       },
       relations:{
           0:{
               parent:'customers', 
               child:'orders', 
               keyparent:'custid', 
               keychild:'orderid',
               onetomany:true
           }
       }
   }
Faults

Whenever there is a problem on the server side, it is a good practice to report an exception in the form of:

   fault = {error:401, text:'Data source not available'};
   fault = {error:501, text:'You have no rights to access that data'};

then, on the client side, eval() and test for errors in the response:

function myStuff(response){
   var data = eval(response);
   if(data.error){
      alert(data.text);
   }else{
     doStuffWith(data);
   }

Well folks, we have covered everything you should know to get you up and running. I hope you have enjoyed this introductory essay on JavaScript Object Literals. George Nava - 2005.11.11

Original Document Information

  • Author: George Nava
  • Last Updated Date: 2005.11.11
  • Original Source: georgenava.com

Page last modified 15:04, 5 May 2006 by Pmash

Tags:

Files (0)