How to access the correct this inside a callback
To rectify this context speedily within a callback, pick arrow functions which lexically bind this, or switch to .bind() for your classic functions.
With arrow functions, you enjoy brevity, while .bind(this) lets you display the this context on its forehead.
Keep this in a variable
In an environment devoid of arrow functions, worry not. The age-old hack is to assign this to another variable, mostly named self or that. Now this is accessible within the callbacks.
This method is especially handy when attempting to bridge the gap between ES5 and ES6.
Carve this with .bind()
Take a stroll with .bind(). This method chisels this into a shape that you desire. It creates a new function that, when you call, adjusts its this keyword according to the value you provided.
thisArg in Array methods: map, forEach, filter
Some Array methods like Array#map, Array#forEach, Array#filter get a kick out of playing with thisArg. This optional parameter strikes a pose, assigning a context to this, right inside the callback function.
Dealing with this in event handlers
In DOM event handlers, this usually signals to the element in receipt of the event. But hey, you can change reference of this just by using .bind().
Calling this in constructor functions
Introduce this to constructor functions in JavaScript, only to find them reciprocating by sharing secrets about this patterns within the constructor. When this isn't the expected instance, keeping this in line calls for arrow functions or .bind().
jQuery's love for this
For jQuery users, call out to .proxy() function to manage this in your callbacks. In $.ajax(), the context option Readies.Set.Goes... your callbacks for the correct this value.
Going back in time to legacy codebases, or specific frameworks? These solutions equip your callbacks for consistent functionality.
Strict 'mode' in ambiguous this
The 'use strict' mode has a thing for changing the behavior of this. In its world, this doesn't default to the global object, an essential nugget to remember.
Advanced this patterns
As you progress, architectures such as modules or factories could lineup for this management.
Follow the script for this: ECMAScript standard
The ECMAScript standard maps the execution context and thisBinding. Grasp the script to uncover more about this.
Was this article helpful?