Page 157 - Computer Software Application TP - Volume 1
P. 157
COMPUTER SOFTWARE APPLICATION - CITS
3 Save the program as a .html file
4 Open the html file with a web browser
5 Go to the browsers console tab and verify the output.
II Using Constructor Functions
1 Open the text editor
2 Write the following codes
<html >
<head>
<title> Constructor Functions </title>
</head>
<body>
<script>
// Constructor function for creating person objects
function Person(firstName, lastName, age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.fullName = function() {
return this.firstName + “ “ + this.lastName;
};
}
// Creating a new instance of Person
var person1 = new Person(“John”, “Doe”, 30);
// Accessing object properties and methods
console.log(person1.firstName); // Output: John
console.log(person1.lastName); // Output: Doe
console.log(person1.age); // Output: 30
console.log(person1.fullName()); // Output: John Doe
</script>
</body>
</html>
142
CITS : IT & ITES - Computer Software Application - Exercise 40