I’ve been brushing up on my Javascript skills lately and came across the .bind() and arrow functions. Firstly, the bind method creates a new function from existing function when it gets called. It also creates a new scope for the newly created function by setting this to whatever is supplied to .bind(this).
To see an example of how one might get the scope of this mixed up, let’s consider this slightly modified example from Mozilla’s Development Network. Let’s open up a js console in chrome.
Another advantage of using bind is the ability to leverage partially applied functions. By abstracting out our arguments supplied in our function and using bind, we can create a new function leveraging the functionality of another. Just for fun let’s write a function that replaces all occurrences of a word in a string with ‘foobar’.
Arrow functions introduced an easier way to write functions. The syntax is as follows
Arrow functions are always anonymous and don’t assign a new ‘this’!