Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Understanding this in Methods | Prototypes, Inheritance, and the this Keyword
Objects and Prototypes in JavaScript

bookUnderstanding this in Methods

Understanding how the this keyword works in JavaScript is crucial for working with object methods. The value of this is not fixed. It is dynamic and determined by how a function is called. In the context of object methods, this usually refers to the object that owns the method. This dynamic nature allows methods to access and manipulate the data stored within their own object.

12345678
const car = { brand: "Toyota", getBrand: function() { return this.brand; } }; console.log(car.getBrand()); // "Toyota"
copy

In the example above, the car object has a property called brand and a method called getBrand. Inside the getBrand method, this.brand refers to the brand property of the car object. When you call car.getBrand(), JavaScript sets this to point to the car object, so the method returns "Toyota".

However, because this is dynamic, its value can change depending on how the method is called. If you assign the getBrand method to another object, this will refer to the new object instead.

1234567891011121314
const car = { brand: "Toyota", getBrand: function() { return this.brand; } }; const bike = { brand: "Trek" }; bike.getBrand = car.getBrand; console.log(bike.getBrand()); // "Trek"
copy
question mark

What will be logged to the console when the code below is run?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 4

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Suggested prompts:

Can you explain more scenarios where the value of `this` changes in JavaScript?

What happens if I call the method without an object, like just `getBrand()`?

Can you show how arrow functions affect the value of `this` in object methods?

Awesome!

Completion rate improved to 7.69

bookUnderstanding this in Methods

Deslize para mostrar o menu

Understanding how the this keyword works in JavaScript is crucial for working with object methods. The value of this is not fixed. It is dynamic and determined by how a function is called. In the context of object methods, this usually refers to the object that owns the method. This dynamic nature allows methods to access and manipulate the data stored within their own object.

12345678
const car = { brand: "Toyota", getBrand: function() { return this.brand; } }; console.log(car.getBrand()); // "Toyota"
copy

In the example above, the car object has a property called brand and a method called getBrand. Inside the getBrand method, this.brand refers to the brand property of the car object. When you call car.getBrand(), JavaScript sets this to point to the car object, so the method returns "Toyota".

However, because this is dynamic, its value can change depending on how the method is called. If you assign the getBrand method to another object, this will refer to the new object instead.

1234567891011121314
const car = { brand: "Toyota", getBrand: function() { return this.brand; } }; const bike = { brand: "Trek" }; bike.getBrand = car.getBrand; console.log(bike.getBrand()); // "Trek"
copy
question mark

What will be logged to the console when the code below is run?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 4
some-alt