Ah, I assume you mean that.js, which isn't a specific library called "that.js" but rather a very common JavaScript pattern used to deal with the tricky behavior of the this keyword.
Let me break it down:
The Problem: this Context in JavaScript
In JavaScript, the value of the this keyword is dynamically determined based on how a function is called, not where it's defined. This can be a major source of confusion, especially when dealing with:
- Callbacks: Functions passed as arguments to other functions (e.g.,
setTimeout, event listeners, array methods like forEach, map).
- Nested functions: Inner functions often have their own
this context.
Inside these scenarios, this often loses its intended value (e.g., the object it was called on) and might default to the global object (window in browsers, undefined in strict mode).
The that.js Pattern (The Solution)
The "that.js" pattern is a convention to capture and preserve the this context from an outer scope so you can use it reliably in an inner scope.
The pattern looks like this:
function MyObject() {
this.value = "Hello";
var that = this; // <-- THE "THAT.JS" PATTERN
// 'that' now permanently holds the 'this' from MyObject's scope
this.printValueDelayed = function() {
setTimeout(function() {
// Inside this callback, 'this' would normally refer to the global object (window)
// or be undefined in strict mode.
// But 'that' still refers to the 'MyObject' instance!
console.log(that.value); // We use 'that' instead of 'this'
}, 1000);
};
this.printValueImmediately = function() {
console.log(this.value); // 'this' is correct here because it's directly called on MyObject
};
}
const obj = new MyObject();
obj.printValueDelayed(); // Output (after 1 second): "Hello"
obj.printValueImmediately(); // Output: "Hello"
Key Idea: You create a variable (commonly named that, self, _this, or _self) and assign the current this value to it before you enter the scope where this would change. Because JavaScript closures remember their outer scope's variables, that will always refer to the correct original this.
Why it's "Less Common" Now (Modern Alternatives)
While var that = this; is still perfectly valid and you'll see it in older codebases, modern JavaScript (ES6 and beyond) provides more elegant solutions:
-
Arrow Functions (=>):
This is the most common and preferred modern solution. Arrow functions do not bind their own this. Instead, they lexically inherit this from their surrounding scope. This means this inside an arrow function will always be the same as this outside it.
function MyObjectModern() {
this.value = "Hello from modern";
this.printValueDelayed = function() {
setTimeout(() => { // <--- Arrow function!
console.log(this.value); // 'this' correctly refers to MyObjectModern instance
}, 1000);
};
}
const objModern = new MyObjectModern();
objModern.printValueDelayed(); // Output (after 1 second): "Hello from modern"
-
bind(), call(), apply():
These are methods on functions that allow you to explicitly set the this context. bind() creates a new function with this permanently set to a specific value.
function MyObjectBind() {
this.value = "Hello from bind";
this.printValueDelayed = function() {
setTimeout(function() {
console.log(this.value);
}.bind(this), // <--- Bind 'this' to the function
1000);
};
}
const objBind = new MyObjectBind();
objBind.printValueDelayed(); // Output (after 1 second): "Hello from bind"
In Summary:
that.js refers to the var that = this; pattern.
- Purpose: To preserve the correct
this context when this would otherwise change (e.g., in callbacks or nested functions).
- Current Status: Largely superseded by ES6 Arrow Functions, which provide a cleaner and more idiomatic way to achieve the same result by inherently preserving lexical
this. You might still see it in legacy code.