HomeThe ClassicsFarai's Codelab

WTF is this in JavaScript?

Published: Updated:

It’s the calling context which depends on where and how it’s called.

new constructs this from a constructor, otherwise it’s the calling context. Rather than this being isolated to the function block, it takes the this from where the function is called. So if it’s at the top level, it inherits the global context, that’s why constructor functions panic without new.

You can specify a custom this with apply and call and the respective Reflect functions.