Question Bank

JavaScript `this` and Arrow Function Code Traces

Difficulty: Medium

Six traces covering lost-`this` on extracted methods, arrow inherits-from-enclosing, function-as-constructor return, IIFE `this`, and static vs prototype methods.

Question Bank
/

JavaScript `this` and Arrow Function Code Traces

JavaScript `this` and Arrow Function Code Traces

Six traces covering lost-`this` on extracted methods, arrow inherits-from-enclosing, function-as-constructor return, IIFE `this`, and static vs prototype methods.

Question Bank
Medium
JavaScript
6 questions
quiz
interview-prep
js-this
js-arrow-functions

1,063 views

34

What does the following code print, and what is the minimal fix?

Examples

Example 1:

Input: sayName()
Output: undefined
Explanation: Extracting student.getName into a bare reference detaches the method, so this is the global object (or undefined in strict mode) and this.name is undefined.