`this` context in JavaScript

Just a guy who loves to write code and watch anime.
Search for a command to run...

Just a guy who loves to write code and watch anime.
No comments yet. Be the first to comment.
Qualities that make outstanding builders.

Bipedal vs quadrupedal: how many legs This is about the number of legs the creature walks on. Bipedal. Two legs. Humans, ostriches, T-rex, kangaroos, most fantasy humanoids. Quadrupedal. Four legs. Do

Intro You hear "lerp" and "smoothstep" everywhere in game dev. They sound like math jargon. They're not. Both are small tools that do the same job: smoothly move from one value to another. The problem

What is Kinematics Kinematics is the math field. It's the study of how things move without worrying about forces (which would be dynamics). FK and IK are the two branches: forward kinematics and inver

Intro Textures are usually the biggest cost in a 3D scene. Memory, bandwidth, and load time all get eaten by them. Resizing them is the obvious lever. There's more. This post is about the less obvious

When used in the global scope, this refers to the global object. In a browser environment, this global object is window, and in Node.js, it is global.
function myFunction() {
console.log(this); // Refers to the global object
}
myFunction();
When this is used inside a method of an object, it refers to the object itself.
const myObject = {
myMethod() {
console.log(this); // Refers to myObject
},
};
myObject.myMethod();
This allows methods to access other properties or methods of the same object using this.
In the context of addEventListener, this refers to the element that the event listener is attached to.
However, this wouldn't work with arrow functions, they'd refer to the global object in this case. (see next section)
const myButton = document.querySelector("button");
function logThis() {
console.log(this); // Refers to the button element
}
myButton.addEventListener("click", logThis);
Arrow functions do not have their own this. Instead, they inherit this from the parent scope at the time they are defined.
const myObject = {
myMethod: () => {
console.log(this); // Inherits `this` from the parent scope
},
};
myObject.myMethod(); // Likely logs the global object or undefined in strict mode
this with bindThe bind method allows you to set the value of this explicitly for any function.
function logThis() {
console.log(this);
}
const myObject = { name: "John" };
const boundFunction = logThis.bind(myObject);
boundFunction(); // `this` will refer to myObject
this with call and applyBoth call and apply methods allow you to call a function with an explicitly set this value. The difference between them lies in how you pass arguments: call takes them individually, while apply accepts them as an array.
function logThis() {
console.log(this);
}
const myObject = { name: "John" };
logThis.call(myObject); // Calls the function with `this` set to myObject
function logThisWithArgs(x, y) {
console.log(this, x, y);
}
logThisWithArgs.apply(myObject, [1, 2]); // Also calls the function with `this` set to myObject, arguments passed as an array
this in Array MethodsSome array methods, like forEach, allow you to specify the value of this to be used within the callback function.
const myArray = [1, 2, 3];
const myObject = { name: "John" };
myArray.forEach(function(item) {
console.log(this, item); // `this` refers to myObject
}, myObject);
However, this does not apply to arrow functions within these methods because they do not have their own this context.