menu

Sunday, August 25, 2013

JavaScript Object

Object is special type of data with properties and methods. Almost every thing in javascript is an object. 

<script type="text/javascript">
    var arr = new Array(10);
    for (var i = 0; i < 10; i++) {
        //arr[i] = { firstname: 'Kunal', age: 25 + i };
        //OR
        var kunal = new Object();
        kunal.firstname = 'Kunal Maurya';
        kunal.age = 20 + i;
        arr[i] = kunal;
    }
    for (var i = 0; i < 10; i++) {
        var kunal = arr[i];
        document.write(kunal.firstname + " is " + kunal.age + " years old.
"
);
        //OR
        //document.write("
" + arr[i].firstname + " is " + arr[i].age + " years old.");
    }

</script>

No comments:

Post a Comment