This program shows how this accesses the title and author properties inside the book object.
Javascript
const book = {
title: 'JavaScript Basics',
author: 'Sam',
describe() {
console.log(`"${this.title}" is written by ${this.author}.`);
}
};
book.describe();
OutputSuccess
Important Notes
Inside object methods, this always points to the object calling the method.
If you use this outside an object or in arrow functions, it may not point to the object you expect.
Summary
this lets an object refer to itself inside its methods.
Use regular functions (not arrow functions) for methods to get correct this.
this helps write cleaner and reusable object code.
Practice
(1/5)
1. What does this refer to inside a regular method of an object in JavaScript?
easy
A. The object itself
B. The global window object
C. Undefined
D. The method's name
Solution
Step 1: Understand this in object methods
Inside a regular function defined as a method of an object, this refers to the object that owns the method.
Step 2: Compare with other contexts
Unlike arrow functions or global functions, regular methods bind this to the object itself.
Final Answer:
The object itself -> Option A
Quick Check:
this in object method = object [OK]
Hint: Remember: regular methods use this as the object [OK]
Common Mistakes:
Thinking this is always global
Confusing arrow functions with regular functions
Assuming this is the method name
2. Which of the following is the correct way to define a method that uses this inside an object?
easy
A. const obj = { greet: () => console.log(this.name) };
B. const obj = { greet: function() => console.log(this.name) };
C. const obj = { greet() { console.log(this.name); } };
D. const obj = { greet: function => console.log(this.name) };
Solution
Step 1: Identify correct method syntax
const obj = { greet() { console.log(this.name); } }; uses the concise method syntax with a regular function, which correctly binds this to the object.
Step 2: Check other options for errors
const obj = { greet: () => console.log(this.name) }; uses an arrow function, which does not bind this to the object. Options B and D have invalid syntax for function definitions.
D. Change arrow function to regular function: showBrand() { console.log(this.brand); }
Solution
Step 1: Identify arrow function issue
Arrow functions do not have their own this. Here, this.brand is undefined because this is not bound to car.
Step 2: Fix by using regular function
Changing showBrand to a regular method binds this to car, so this.brand correctly accesses 'Toyota'.
Final Answer:
Change arrow function to regular function: showBrand() { console.log(this.brand); } -> Option D
Quick Check:
Regular function binds this correctly [OK]
Hint: Avoid arrow functions for object methods needing this [OK]
Common Mistakes:
Using arrow functions for methods
Trying to bind this on arrow functions
Ignoring this context rules
5. You want to create an object with a method that returns a greeting using the object's name. Which code correctly uses this to achieve this?
const user = {
name: 'Bob',
greet: ???
};
console.log(user.greet());
hard
A. greet: () => `Hello, ${this.name}!`
B. greet() { return `Hello, ${this.name}!`; }
C. greet: function() => `Hello, ${this.name}!`
D. greet: function { return `Hello, ${this.name}!`; }
Solution
Step 1: Check method syntax and this binding
greet() { return `Hello, ${this.name}!`; } uses a regular method syntax, so this correctly refers to the user object.
Step 2: Validate other options
greet: () => `Hello, ${this.name}!` uses an arrow function, which does not bind this to the user object. Options B and C have invalid syntax for function definitions.
Final Answer:
greet() { return `Hello, ${this.name}!`; } -> Option B
Quick Check:
Regular method with this = greet() { return `Hello, ${this.name}!`; } [OK]
Hint: Use regular methods for this access in objects [OK]
Common Mistakes:
Using arrow functions for methods
Incorrect function syntax
Expecting this to work in arrow functions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.