Skip to content

Instantly share code, notes, and snippets.

@thatPamIAm
Last active March 14, 2019 17:43
Show Gist options
  • Save thatPamIAm/f7d8686d16adeb850226a9bbc9dcb69f to your computer and use it in GitHub Desktop.
Save thatPamIAm/f7d8686d16adeb850226a9bbc9dcb69f to your computer and use it in GitHub Desktop.
Understanding keyword `this` Warm Up

Warm Up

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

RULE 1

Is the function called with new (new binding)? => this is the newly constructed object

RULE 2

Is the function called with call, apply, or bind? => this is the explicitly specified object.

RULE 3

Is the function called as a method within an object (implicit binding) => this is that context object.

RULE 4

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment