Page 156 - Computer Software Application TP - Volume 1
P. 156

COMPUTER SOFTWARE APPLICATION - CITS



           EXERCISE 40 : Creating and using Objects in JavaScript


            Objectives

           At the end of this exercise you shall be able to
           •  create objects
           •  use document object model.

           Requirements


           Tools/Materials
           •   Desktop / Laptop with latest configuration
           •   Text editor
           •  Web browser

           Procedure


           TASK 1: Creating an Object
           I  Using Object Literal Notation
              1  Open the text editor

              2  Write the following codes
                 <html >
                 <head>
                 <title> Object Literal Notation </title>
                 </head>
                 <body>
                 <script>
                 // Creating an object using object literal notation
                 var person = {
                 firstName: “John”,
                 lastName: “Doe”,
                 age: 30,
                 fullName: function() {
                 return this.firstName + “ “ + this.lastName;
                 }
                 };

                 // Accessing object properties and methods
                 console.log(person.firstName); // Output: John
                 console.log(person.lastName); // Output: Doe
                 console.log(person.age); // Output: 30

                 console.log(person.fullName()); // Output: John Doe
                 </script>
                 </body>
                 </html>





                                                           141
 CITS : IT & ITES - Computer Software Application - Exercise 39
   151   152   153   154   155   156   157   158   159   160   161