Understanding <code>this</code> in JavaScript Functions
📖 Scenario: Imagine you are building a simple app that keeps track of a user's name and can greet them. You want to understand how the keyword this works inside functions to access the user's name.
🎯 Goal: You will create an object representing a user with a name property and a function that uses this to greet the user by name.
📋 What You'll Learn
Create an object called user with a property name set to 'Alice'
Add a method called greet inside user that returns a greeting string using this.name
Call the greet method and print the result
💡 Why This Matters
🌍 Real World
Understanding <code>this</code> is important when working with objects and methods in JavaScript, which is common in web apps and interactive websites.
💼 Career
Many programming jobs require working with JavaScript objects and functions where <code>this</code> is used to access data inside objects, so mastering this concept helps you write better code.
Progress0 / 4 steps
1
Create the user object with a name property
Create an object called user with a property name set to the string 'Alice'.
Javascript
Hint
Use const user = { name: 'Alice' }; to create the object.
2
Add a greet method using this.name
Add a method called greet inside the user object that returns the string `Hello, ${this.name}!` using this.name.
Javascript
Hint
Define greet() as a function inside user that returns the greeting string using this.name.
3
Call the greet method and store the result
Call the greet method on the user object and store the result in a variable called message.
Javascript
Hint
Use const message = user.greet(); to call the method and save the greeting.
4
Print the greeting message
Print the value of the variable message to the console.
Javascript
Hint
Use console.log(message); to show the greeting in the console.
Practice
(1/5)
1. What does this refer to inside a regular function called as obj.method()?
easy
A. The function itself
B. The global object (like window)
C. Undefined
D. The object obj that calls the method
Solution
Step 1: Understand how this works in method calls
When a function is called as a method of an object, this points to that object.
Step 2: Apply to the example obj.method()
Since method is called by obj, this inside method refers to obj.
Final Answer:
The object obj that calls the method -> Option D
Quick Check:
obj.method() means this = obj [OK]
Hint: Remember: this is the caller object in method calls [OK]
Common Mistakes:
Thinking this is always global
Confusing this with the function itself
Assuming this is undefined in method calls
2. Which of the following is the correct way to define a method inside an object so that this refers to the object?
Arrow functions do not have their own this; they inherit from the outer scope. Regular functions have their own this.
Step 2: Check each option
A uses arrow function, so this is inherited from outer scope (global), not the object. B uses concise method syntax, which binds this to the object. C uses invalid syntax (cannot define concise method with arrow). D uses invalid syntax (function() => not valid).
C. Assign const self = this and use self.name inside inner
D. Change inner to an arrow function
Solution
Step 1: Understand why this is wrong inside inner
Because inner is a regular function called alone, this is undefined, not obj.
Step 2: Explore fixes to bind this correctly
Change inner to an arrow function: Using arrow function for inner inherits this from greet, which is obj. Bind inner to this using inner.bind(this)(): Using inner.bind(this)() explicitly sets this to obj. Assign const self = this and use self.name inside inner: Saving this in self and using self.name works because self is accessible inside inner.
Step 3: Conclusion
All three methods correctly fix the problem.
Final Answer:
All of the above -> Option B
Quick Check:
Bind or arrow or self variable fixes this [OK]
Hint: Use arrow, bind, or self variable to fix this in inner functions [OK]
Common Mistakes:
Only trying one fix and missing others
Not understanding arrow functions inherit this
Forgetting to call bind method
5. You want to create an object counter with a method count that increases a number every second and logs it using this.value. Which code correctly keeps this referring to counter inside setInterval callback?
B. Bind this to the callback: setInterval(function() { ... }.bind(this), 1000);
C. Save const self = this before setInterval and use self.value inside callback
D. Replace the callback with an arrow function: setInterval(() => { this.value++; console.log(this.value); }, 1000);
Solution
Step 1: Identify the problem with this in setInterval
The callback function is a regular function, so this inside it is not counter but undefined or global object.
Step 2: Explore solutions to keep this as counter
Replace the callback with an arrow function: setInterval(() => { this.value++; console.log(this.value); }, 1000);: Arrow functions inherit this from count, so this.value works. Bind this to the callback: setInterval(function() { ... }.bind(this), 1000);: Using bind(this) explicitly sets this inside callback. Save const self = this before setInterval and use self.value inside callback: Saving this in self and using self.value works because self is accessible inside callback.
Step 3: Conclusion
All three methods correctly keep this referring to counter.
Final Answer:
All of the above -> Option A
Quick Check:
Arrow, bind, or self variable all fix this in callbacks [OK]
Hint: Use arrow, bind, or self variable to keep this in callbacks [OK]
Common Mistakes:
Using regular function without binding this
Expecting this to be counter automatically
Not saving this before callback
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.