Don’t fail to answer this `this` question at interview

Photo by Cytonn Photography on Unsplash

Let’s see the code:

What will output? The answer is Bob(immediately) … Bob(after 3s).

In a function, this is determined by how a function is called (runtime binding). It can’t be set by assignment during execution, and it may be different each time the function is called.

In this example, neither of the function assign a this (by call, bind or apply) when called, so both of this default to the global object ( window ).

However, if we log this inside test2 , thing’s gonna be different.

In this example, we log directly.

In test1 , this is the upper scope(obj), so is John .

In test2 , arrow functions don’t have their own this , so this points to the global object(window), and we get Bob .




Web Developer in Tiktok. Write about git, js, interview, etc.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Angular Change Detection and OnPush Strategy

All you need to know about Modern JavaScript

Manage code quality and static code check in any big javascript project| Set right governance for…

Code Quality — Real Test

Axios POST method in Vue JS with Laravel Back-end

Workflow of Sketch-FX EX in SketchUp

TIL 2020.12.01

Case Studies for Common Pitfalls That JavaScript Developers Should Know

[Learning]#25 JS: HTML DOM Events — Part 3(Start Over Version)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


Web Developer in Tiktok. Write about git, js, interview, etc.

More from Medium

5 Questions Junior Developers Should Ask Themselves Before Choosing A Company

Problem-solving: How to create an excerpt

How to determine the minimum number of rooms that are required for the below meetings arrays…

[LeetCode] (Easy) 53. Maximum Subarray