Page 207 - Web_Application_v2.0_C12_Fb
P. 207
Anonymous Function
A function without a name, usually assigned to a variable.
The syntax to create anonymous function is as follows:
var variableName = function() {
// code block
};
variableName();
Example 16: To demonstrate the use of anonymous function
<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE>Anonymous Function in JavaScript</TITLE>
</HEAD>
<BODY>
<SCRIPT>
var displayMessage = function() {
document.write("This is an Anonymous Function Example!");
};
displayMessage();
</SCRIPT>
</BODY>
</HTML>
Output:
Constructor Function
A constructor function is a special type of function used to create and initialize objects. It acts as a blueprint
for creating multiple objects with the same properties and methods.
The syntax to create Constructor Functions is as follows:
function ConstructorName(parameters) {
this.property = parameters;
}
var objectName = new ConstructorName(value);
Example 17: To demonstrate the use of constructor function
<!DOCTYPE HTML>
<HTML>
JavaScript Part 2 205

