Introduction
The this keyword helps you access the current object or context inside a function or method.
The this keyword helps you access the current object or context inside a function or method.
function example() { console.log(this); }
this value depends on how the function is called, not where it is defined.
In global code, this usually refers to the global object (like window in browsers).
this refers to obj, so it prints 'Alice'.const obj = { name: 'Alice', greet() { console.log(this.name); } }; obj.greet();
this is the global object. In strict mode, it is undefined.function show() { console.log(this); } show();
this inside the class refers to the instance p.class Person { constructor(name) { this.name = name; } sayName() { console.log(this.name); } } const p = new Person('Bob'); p.sayName();
This program shows how this refers to the car object inside the method.
const car = { brand: 'Toyota', showBrand() { console.log(`This car is a ${this.brand}`); } }; car.showBrand();
Arrow functions do not have their own this; they use this from the surrounding code.
Be careful when extracting methods from objects; this may lose its original meaning.
this points to the current object or context.
Its value depends on how a function is called.
It helps write flexible and reusable code.
this keyword represent inside a regular function in JavaScript when called as a method of an object?this refers to that object.this points to the object owning the method, not the global object or function itself.this in method = owning object [OK]this is the object before the dot [OK]this always points to global objectthis with the function itselfthis is undefined in methodsthis inside an arrow function to refer to the surrounding object?this behaviorthis; they inherit it from the surrounding scope.this inside an arrow function refers to the this of the outer function or object context.this, so it inherits from the surrounding scope -> Option Dthis = inherited context [OK]this from outside [OK]thisthis to be the objectthis with regular function thisconst obj = {
name: 'Alice',
greet() {
console.log(this.name);
}
};
const greetFunc = obj.greet;
greetFunc();greetFunc() is called?obj.greet to greetFunc detaches the method from its object.greetFunc()this inside greetFunc is the global object or undefined in strict mode, so this.name is undefined.this = undefined [OK]this [OK]this to still point to the objectthisconst person = {
name: 'Bob',
sayName: () => {
console.log(this.name);
}
};
person.sayName();this behaviorthis; they inherit from the outer scope, which is not person.console.log(this.name)this is not person, this.name is undefined or from global scope, not 'Bob'.this inside arrow function does not refer to person -> Option Athis ≠ object [OK]this [OK]this to objectthis issuesCar that sets the make property using this. Which code correctly uses this to assign the property when creating a new car?this to assign properties to the new object created by new.this.make = make and uses new to create an object. function Car(make) { make = this; } const myCar = Car('Toyota'); assigns incorrectly. const Car = (make) => { this.make = make; }; const myCar = new Car('Toyota'); uses arrow function which does not bind this. function Car(make) { this.make = make; } const myCar = Car('Toyota'); calls constructor without new, so this is not the new object.this needs new [OK]new with constructor to bind this correctly [OK]newmake = this instead of this.make = makePass the test and this pattern moves unknown