DOM - Events Flashcards

How the DOM communicates with JavaScript via Events

1
Q

What are the different types of events through which the DOM communicates with JavaScript?

A

The DOM communicates with JavaScript through various event types including mouse-related events (movement, click, enter/leave), keyboard-related events (down, up, press), focus-related events (focus in, focus out), changes in input fields, form submissions, timer events, and miscellaneous events such as content changes, page load/unload, image load, and uncaught exceptions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do mouse-related events differ from keyboard-related events in the context of DOM communication?

A

In the context of DOM communication, mouse-related events primarily deal with user interactions involving the mouse, such as movement, clicks, and hovering over elements. Keyboard-related events, on the other hand, involve interactions with the keyboard, like key presses, releases, and holding down keys. Both types of events provide specific information relevant to their context, enabling JavaScript to respond to different user actions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the three components necessary for creating an event handler in the DOM?

A

The three components necessary for creating an event handler in the DOM are: 1. Identifying the event of interest (e.g., click, mouseover), 2. Selecting the element of interest, and 3. Defining the JavaScript to be executed when the event occurs on the element.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How does specifying an event, an element, and a JavaScript action form the basis of event handling in web applications?

A

Specifying an event, an element, and a JavaScript action forms the basis of event handling by allowing web applications to dynamically respond to user interactions. The event specifies what to listen for, the element defines where to monitor for the event, and the JavaScript action determines the response when the event occurs. This interaction creates a responsive and interactive user experience.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the two primary ways to specify JavaScript for handling an event in the DOM?

A

The two primary ways to specify JavaScript for an event in the DOM are: 1. In the HTML, using inline event handlers like <div>, and 2. From JavaScript using the DOM, either by assigning a function directly to the event property (e.g., element.onclick = mouseClick) or using element.addEventListener(“click”, mouseClick).</div>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How does using element.addEventListener differ from setting an event property like element.onclick?

A

Using element.addEventListener allows for more flexibility compared to setting an event property like element.onclick. With addEventListener, multiple event handlers can be attached to a single event, and it provides more options, like capturing and bubbling phases. On the other hand, setting an event property directly, like element.onclick, only allows for one event handler and overwrites any existing handler for that event.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is an Event object in the context of DOM events, and what are some of its key properties?

A

In the context of DOM events, an Event object is passed to event listener functions and contains details about the event. Key properties include type (name of the event), timeStamp (time of event creation), currentTarget (element where listener is registered), and target (element dispatching the event).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do the properties currentTarget and target differ in an Event object?

A

In an Event object, currentTarget refers to the element on which the event listener is actually registered, while target refers to the element that dispatched the event. This distinction is important for events that bubble, as currentTarget remains constant during the bubbling, but target can vary if different elements trigger the same event listener.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are some key properties of MouseEvent and KeyboardEvent in the DOM?

A

Key properties of MouseEvent include button (mouse button pressed), pageX and pageY (mouse position relative to the document), and screenX and screenY (mouse position in screen coordinates). KeyboardEvent properties include keyCode (identifier for the pressed key) and charCode (Unicode value for the keypress, if applicable).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do the properties of MouseEvent differ from those of KeyboardEvent, and why are these differences significant?

A

The properties of MouseEvent and KeyboardEvent differ based on the nature of the interactions they represent. MouseEvent properties relate to the mouse’s position and buttons, suitable for tracking user actions with the mouse. KeyboardEvent properties focus on key identification and Unicode values, which are essential for interpreting keyboard interactions. These differences are significant as they tailor the event information to the specific interaction type, whether it’s with a mouse or keyboard.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Compare and contrast the two methods of implementing a draggable rectangle: one using inline HTML event handlers and the other using JavaScript event listeners.

A

The first method uses inline HTML event handlers (onmousedown, onmousemove, onmouseup) directly within the HTML element. This approach tightly couples the HTML structure with JavaScript functionality, making the code less modular. The second method separates concerns by using JavaScript to attach event listeners. This approach enhances maintainability and scalability, as event handling logic is encapsulated within JavaScript functions. It allows for more complex interactions and conditions, like tracking whether the mouse is down. While both methods achieve similar functionality, the JavaScript event listener approach offers better separation of concerns and is more adaptable to complex scenarios.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

In the context of nested elements, how is it determined which event handlers are invoked for an event?

A

In nested elements, event handlers invocation depends on event propagation, either bubbling (from inner to outer elements) or capturing (from outer to inner). The event starts from the innermost element and can propagate up to outer elements, invoking handlers at each level. Developers can decide at which level to handle the event.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Explain the advantages and use cases for handling an event at the innermost element versus an outer element.

A

Handling an event at the innermost element provides specificity and control, useful when the action is related only to that specific element. In contrast, handling events at outer elements is advantageous for more generalized actions or when managing similar events from multiple child elements. The choice depends on the desired scope and granularity of the event handling.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Explain the difference between the Capture phase and the Bubble phase in event propagation.

A

In event propagation, the Capture phase starts at the outermost element and works down to the innermost, allowing handlers to act before the event reaches the target. The Bubble phase starts at the target and propagates up to the outermost element, which is the default for most event handlers. Handlers can be attached to each phase, and events can be stopped using event.stopPropagation().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Why are event handlers in the Bubble phase more common than in the Capture phase?

A

Event handlers in the Bubble phase are more common because this phase starts from the event target and moves outward, which aligns with the intuitive understanding of how events propagate. It’s more practical for most use cases, as it allows handling events after they’ve been triggered by the target element. In contrast, the Capture phase is used less frequently as it involves handling events before they reach the target.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Explain how timer events like setTimeout and setInterval are used in the DOM.

A

In the DOM, setTimeout is used to execute a function once after a specified delay, while setInterval is used to execute a function at regular intervals. Both return a token, which can be used with clearInterval or clearTimeout to cancel the timer.

17
Q

What are some common use cases for timer events in web development?

A

Common use cases for timer events in web development include creating animations, implementing automatic page refreshes, delaying actions until a specific time has elapsed, and periodically checking or updating data without user intervention.

18
Q

How are events processed in terms of concurrency in the DOM?

A

In the DOM, events are processed in a serialized, one-at-a-time manner, without interleaving with other JavaScript execution. Event handlers run to completion before any other scripts or handlers, ensuring no multi-threading and simplifying concurrency management.

19
Q

What are the implications of the DOM’s single-threaded event handling for background processing?

A

The single-threaded nature of the DOM’s event handling makes background processing more challenging, as there are no separate threads to handle tasks concurrently. This means long-running event handlers can block other scripts, impacting the user experience. Developers often rely on asynchronous programming patterns like promises and async/await to manage background tasks effectively.

20
Q

What is event-based programming, and what are its key characteristics in the context of the DOM?

A

Event-based programming involves writing code that runs in response to events. Key characteristics include waiting for events to invoke code, needing handlers to return quickly to avoid locking up the application, and maintaining control through sufficient event handlers and timers.

21
Q

How does event-based programming impact the development of server-side applications in Node.js?

A

Event-based programming is central to Node.js, impacting server-side development by enabling efficient handling of numerous simultaneous connections through non-blocking, asynchronous operations. This model allows Node.js applications to efficiently manage resources and scale effectively, particularly in I/O-intensive scenarios.