The Four Rules of This
I’m going to print these out, laminate them, and never worry about this again.
- Constructor binding - when the call site is a constructor call using the
newkeyword,thiswill refer to the new object created (see below notes on new keyword). - Explicit binding - when the call site uses
.call()or.bind(),thiswill refer to the object parameter passed. - Implicit binding - when the call site is an object calling a reference to a property function,
thiswill refer to the calling object. - Default binding - when the call site is a function reference with no binding,
thiswill refer to global scope if not in strict mode. In strict mode,thiswill be undefined.