Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте 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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 4

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Awesome!

Completion rate improved to 7.69

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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 4
some-alt