Tuesday, 2 December 2014

Use of Javascript Objects

The javascript objects acts as java beans. which is useful to store information & retrieve the information as easy without any objects.

Creation of javascript object:

var ob = new Object();

ob is an object. present i have to add some properties to this object like name & nickname.

ob.name = "Vamsi";
ob.nickname = "Krishna"

if you print these object properties like this:

console.log(ob.name);
console.log(ob.nickname);

How to maintain number of objects / records. let us see..

Take one empty arraylist add these objects to that array. simple..

var recordsList = [];
for(var i=0;i < 5;i++){
           // Here for every time created a new Object.
           var record = new Object();
           // Decide your properties
          record.uniqueRow = i;
          record.name = "Vamsi "+i;
          // Add this to arraylist
        recordsList.push(record);
}

//Print final arraylist
console.log(recordsList);  // Those are in object format

It reduces lot of java coding. the data easily prepared..

Happy Coding.. Dont do hardwork.. do smart work..

No comments:

Post a Comment