logo
logo

Arrow Functions with Context

Beginner

Arrow Functions with Context

Write an arrow function called
printName
that logs the
name
property of a given object to the console. The function should use the
call()
method to execute in the context of the object.
Console
Submit
Solution
00:00

Solution Walkthrough for Arrow Functions with Context

We use the
call()
method to execute the
printName
function in the context of the object, so that when
printName.call(person)
is called, the function is executed in the context of the
person
object and logs "John Doe" to the console.
Note that in arrow functions, the
this
keyword is inherited from the parent scope and is not redefined. Therefore, in order to use the
call()
method to execute an arrow function in a specific context, we need to define the arrow function using the
function
keyword, or use other methods such as
bind()
or
apply()
.