JavaScript - this key Word Tutorial Points (2)
Understand the ‘this’ keyword in JavaScript.
a. Declare a global variable named vehicle name in the window object.
b. Declare a method named printVehicleName to print out the vehicle name.
c. Declare an object named Vehicle(using object literal notation) which have a variable called vehicle name and declare a function named getVehicleName and assign it with the printVehicleName.
d. Execute the printVehicleName function and the getVehicleName functions to see the results.
e. Correct the getVehicleName to print out the global variable vehicle name using the this keyword
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="XX-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AF LAB 01 - Ex 03</title>
</head>
<body>
<script>
//Understand the ‘this’ keyword in JavaScript.
//a)Declare a global variable named vehicleName in the window object.
window.vehicleName = "Toyota";
//b)Declare a method named printVehicleName to print out the vehicle name
let printVehicleName = function(){
document.write(`<h1> ${window.vehicleName} </h1>`);
}
printVehicleName();
/* c) Declare an object named Vehicle(using object literal notation) which have a
variable called vehicleName and declare a function named getVehicleName and
assign it with the printVehicleName.*/
let Vehicle = {
vehicleName : "Nisan",
getVehicleName : function(){
return `${this.vehicleName} `;
}
}
document.write("<h1>" + Vehicle.getVehicleName()+"</h1>");
</script>
</body>
</html>
OutPut :
Comments
Post a Comment