Write down everything that you know about this
in JavaScript
Where have you seen/utilized the word this
in your JavaScript projects thus far?
- Used this in classes.... working with mythical creatures
- There is implicit binding/ explicit/ default binding
- This refers to the current context of the code
Is the function called with new (new binding)? => this
is the newly constructed object
Is the function called with call, apply, or bind? => this
is the explicitly specified object.
Is the function called as a method within an object (implicit binding) => this
is that context object.
Do no other rules apply? => default this
(default binding) is the global object (undefined
in strict mode)
THERE ARE EXCEPTIONS:
- indirect references to functions
- lexical
this
(working with es6 arrow functions) - event handlers (JS libraries like jquery will often force callbacks to have a
this
that may reference something you don't expect)