Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære 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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 4

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Awesome!

Completion rate improved to 7.69

bookUnderstanding this in Methods

Stryg for at vise menuen

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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 4
some-alt