Page 208 - Web_Application_v2.0_C12_Fb
P. 208
<HEAD>
<TITLE>Constructor Function in JavaScript</TITLE>
</HEAD>
<BODY>
<SCRIPT>
function Car(brand, model, year) {
this.brand = brand;
this.model = model;
this.year = year;
this.details = function() {
document.write("Car: " + this.brand + " " + this.model + " (" + this.year + ")");
}};
var myCar = new Car("Maruti Suzuki", "Swift", 2025);
myCar.details();
</SCRIPT>
</BODY>
</HTML>
Output:
Invoking a Function
In JavaScript, invoking a function means calling it to perform a specific task. When “something” invokes (calls)
the function, the code inside it will run. This happens in the following scenarios:
When an event happens (e.g., a user clicks a button)
When JavaScript code calls (invokes) it
Automatically (self-invoked)
The syntax to invoke a function is as follows:
function functionName(parameters) {
// Code to execute
}
functionName(arguments); // Invoking the function
Example 18: To demonstrate how to invoke a function
<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE>Invoking a function </TITLE>
</HEAD>
<BODY>
206 Touchpad Web Applications (Ver. 2.0)-XII

