Use Arrow Function as a Getter in JavaScript [Tech Interview Quiz]
With the advent of ES6, arrow functions have conquered the JavaScript world.
Consider the following code snippet and determine the result.
Code snippet as a picture
|
|
Are there any issues with the getId
function? What will be logged to the console? (think well, then click for the answer and explanation)
Explanation
The function
getId
is an arrow function, so it doesn’t havethis
of its own.It’s not bound to
this
of theobj
object and when we try to getthis.id
it will be evaluated toundefined
and not1
.
Answer
undefined
will be logged to the console