Semester
Subject
Year
Tribhuwan University
2076
Bachelor Level / Third Year / Fifth Semester / Science
(Web Technology)
Full Marks: 60
Pass Marks: 24
Time: 3 Hours
Candidates are required to give their answers in their own words as for as practicable.
The figures in the margin indicate full marks.
Long Answers Questions
An object in JavaScript is a collection of key-value pairs (properties and methods) that represents a real-world entity.
There are several ways to create objects in JavaScript:
The simplest and most common way to create an object.
var student = {
name: "John",
age: 20,
display: function() {
document.write(this.name);
}
};
new Object()Creates an empty object and adds properties one by one.
var student = new Object();
student.name = "John";
student.age = 20;
A reusable template to create multiple objects of the same type.
function Student(name, age) {
this.name = name;
this.age = age;
}
var s1 = new Student("John", 20);
var s2 = new Student("Alice", 22);
Object.create() MethodCreates a new object with a specified prototype object.
var person = { greet: function() { return "Hello"; } };
var student = Object.create(person);
student.name = "John";
class Student {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
var s1 = new Student("John", 20);
The onmouseover event fires when the mouse pointer enters an element, and onmouseout fires when it leaves.
<!DOCTYPE html>
<html>
<head>
<title>Image Description Changer</title>
<script type="text/javascript">
function changeDescription() {
document.getElementById("desc").innerHTML =
"You are hovering over the image! This is a beautiful flower.";
}
function restoreDescription() {
document.getElementById("desc").innerHTML =
"This is an image of a flower. Move your mouse over it to know more.";
}
</script>
</head>
<body>
<div id="myDiv" style="text-align:center; border:1px solid black; padding:20px;">
<img src="flower.jpg"
alt="Flower Image"
width="300" height="200"
onmouseover="changeDescription()"
onmouseout="restoreDescription()" />
<p id="desc">
This is an image of a flower. Move your mouse over it to know more.
</p>
</div>
</body>
</html>
<div>) — Contains the image and paragraph together as a block<img>) — Displays the flower image with event attributes attachedonmouseover="changeDescription()" — Calls the function when mouse enters the imageonmouseout="restoreDescription()" — Calls the function when mouse leaves the imagedocument.getElementById("desc") — Accesses the paragraph element by its ID.innerHTML — Changes the text content of the paragraph dynamicallyJavaScript provides multiple ways to create objects (literal, constructor, Object.create(), class). Combined with DOM manipulation and event handling (onmouseover, onmouseout), JavaScript enables dynamic and interactive web pages where content changes based on user actions.
Short Answers Questions