Challenge - 5 Problems
Arrow Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of this in arrow function inside object method
What is the output of the following code?
const obj = {
value: 42,
getValue: () => this.value
};
console.log(obj.getValue());Javascript
const obj = { value: 42, getValue: () => this.value }; console.log(obj.getValue());
Attempts:
2 left
💡 Hint
Remember arrow functions do not have their own 'this'.
✗ Incorrect
Arrow functions do not have their own 'this'. Here, 'this' refers to the outer scope, which is the global object or undefined in strict mode. Since 'value' is not defined there, the result is undefined.
❓ Predict Output
intermediate2:00remaining
this in arrow function inside regular function
What will be logged by this code?
function Person() {
this.age = 25;
this.getAge = () => this.age;
}
const p = new Person();
console.log(p.getAge());Javascript
function Person() { this.age = 25; this.getAge = () => this.age; } const p = new Person(); console.log(p.getAge());
Attempts:
2 left
💡 Hint
Arrow function inherits 'this' from the surrounding function.
✗ Incorrect
Inside the constructor, 'this' refers to the new object. The arrow function captures this 'this', so 'this.age' is 25.
❓ Predict Output
advanced2:00remaining
this in nested arrow functions
What is the output of this code?
const obj = {
value: 10,
method() {
const arrow1 = () => {
const arrow2 = () => this.value;
return arrow2();
};
return arrow1();
}
};
console.log(obj.method());Javascript
const obj = { value: 10, method() { const arrow1 = () => { const arrow2 = () => this.value; return arrow2(); }; return arrow1(); } }; console.log(obj.method());
Attempts:
2 left
💡 Hint
Arrow functions inherit 'this' from the surrounding context.
✗ Incorrect
Both arrow functions inherit 'this' from 'method', which is called on 'obj'. So 'this.value' is 10.
❓ Predict Output
advanced2:00remaining
this in arrow function used as event handler
What will this code log when the button is clicked?
const button = document.createElement('button');
button.textContent = 'Click me';
document.body.appendChild(button);
button.addEventListener('click', () => {
console.log(this);
});Javascript
const button = document.createElement('button'); button.textContent = 'Click me'; document.body.appendChild(button); button.addEventListener('click', () => { console.log(this); });
Attempts:
2 left
💡 Hint
Arrow functions do not have their own 'this', so it uses the outer scope's 'this'.
✗ Incorrect
The arrow function uses the 'this' from the outer scope, which is the global Window object in browsers.
🧠 Conceptual
expert2:00remaining
Why arrow functions are not suitable as object methods
Why should arrow functions generally not be used as methods inside objects?
Choose the best explanation.
Choose the best explanation.
Attempts:
2 left
💡 Hint
Think about how 'this' behaves differently in arrow functions.
✗ Incorrect
Arrow functions inherit 'this' from their surrounding scope, so inside an object method, 'this' will not refer to the object, causing unexpected behavior.