In JavaScript, the this keyword is a fundamental concept that can be confusing to first-term students.
It refers to the context in which the current code is executing, and its value can change based on how and where a function is called.
Understanding this is crucial for effectively managing and manipulating both the HTML Document Object Model (DOM) and the JavaScript Browser Object Model (BOM).
What is this?
In JavaScript, this refers to the object that the function is a method of. The value of this is determined at runtime, depending on the execution context:
Global context: In the global execution context (outside of any function), this refers to the global object whether in strict mode or not.
Function context: Inside a function, the value of this depends on how the function was called. If it was called as a method of an object, this is set to the object the method is called on.
Examples Demonstrating this in Different Contexts
1. Global Context
javascript
Copy code
console.log(this.document === document); // true
console.log(this); // Logs global object (window in browser)
Here, this refers to the global window object, under which all global variables and browser-specific objects reside.
2. Function Context
javascript
Copy code
function show() {
console.log(this);
}
show(); // In a browser, this will log the Window object.
In non-strict mode, a regular function call like this has this pointing to the global object (window). In strict mode, this would be undefined.
3. Object Method
javascript
Copy code
const person = {
firstName: "James",
lastName: "T. Kirk",
fullName: function() {
return `${this.firstName} ${this.lastName}`;
}
};
console.log(person.fullName()); // James T. Kirk
In the method call of an object, this refers to the object the method is called on.
How Arrow Functions Affect this
Arrow functions do not have their own this context; instead, they inherit this from the surrounding code where they are defined, which is known as lexical scoping.
console.log(this); // Logs the window object, not the button
});
Here, the arrow function inherits this from its surrounding scope, which is the global window object in a browser, unlike a regular function which would have this set to the element that triggered the event (e.g., the button).
Example with BOM
javascript
Copy code
setTimeout(() => {
console.log(this); // Still window object
}, 1000);
Even inside a setTimeout (which is part of BOM), the arrow function keeps this from its lexical context, unlike a regular function which would have this set to the global object (or undefined in strict mode).
Conclusion
Understanding how this works in different contexts is crucial for programming effectively in JavaScript, especially when manipulating the DOM and handling events in the BOM. By using arrow functions, you can avoid common pitfalls related to this, making your code cleaner and more intuitive. Now, let's practice what we've learned with some exercises that reinforce these concepts.
Let's practice what we've learned with some exercises that reinforce these concepts => present 10 drills with HTML DOM and JavaScript BOM
Practical Drills: Reinforcing the Concepts of this and Arrow Functions in DOM and BOM
These exercises are designed to help first-term students deepen their understanding of the this keyword in various contexts, specifically in interactions with the HTML DOM and JavaScript BOM. The exercises will also incorporate the use of arrow functions to highlight their behavior in these contexts.
Drill 1: DOM Button Click
Objective: Understand how this behaves in event handlers.
Task:
Create an HTML button with the text "Click Me".
Attach an event listener using a regular function and log this.
Repeat with an arrow function and compare the outputs.
These drills are designed to provide a hands-on approach to mastering JavaScript's this keyword in various scenarios involving both the HTML DOM and the JavaScript BOM, reinforcing the concept through practical application.
Want to print your doc? This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (