Wednesday, February 13, 2008

JSON's review

In javascript we can create object in two ways...

1. var newobj=new obj();

2. var newobj={ no:"2333",

name:"xxxx";

favorites:["books",{apple: "yes",mango: "yes"},"car];

display: function()

{

with(this) document.write(" No : "+no+" Name :"+name);

}

};

In the first, I have created the object using new keyword like many other OO programming does.

In the second, I have created the object structure using JSON.

Object is represented by {}

Array is represented by []

Each element in object is indexed by key and has n number data members and functions separated by comma (,).

Array has only value, so we have to use the numeric index to get the value.

Example:

To get the name in newobj... then

newobj.name

To call the function display in newobj... then

newobj.display();

To access first element in favorite array... then

newobj.favorites[0];

No comments: