JS FUNCTIONSChapter 5 · JS Functions and Classes
Understanding this
this is decided by how a function is called. Called as obj.method(), this is obj. Pull the method out and call it on its own, and this is lost. Arrow functions do not have their own this; they use the this of the code around them, which makes them ideal for callbacks inside methods. fn.bind(obj) returns a copy of a function with this locked to obj.
Worked example
How it reads
ship.greet()is called on ship, sothisis ship- The arrow callback in
countdownborrowsthisfrom the method bindmakes a new function whosethisis fixed
Common mistakes
- A regular
functioncallback inside a method gets its ownthis, sothis.countno longer points at your object.

Cloud tip: Inside a method, use an arrow function for any callback that needs
this.

