A lightweight, standalone JavaScript library to soothe your troubled soul

Download That.js:

1.0.2 LTS Latest version

About That.js

That.js is a minimalist JavaScript library that adds support for the long-missing "that" keyword. It's designed for the developer concerned with the duality at the heart of being: identity and non-identity, essence and existence, yin and yang, zero and one. ES6, released in 2015, acknowledged this distinction with the introduction of new keywords let (representing being for-itself, that which is but could just as easily not be) and const (representing being in-itself—that which simply and immutably is). But what about this? Following Sartrean negation, something is only ever "this", insofar as it is not some other thing—i.e. "this" as opposed to "that". At any given point, the specific object to which "this" refers is dependent on its immediate context, but we can know for certain that this is never that (and vice versa). By implementing the that keyword, That.js models this fundamental ontological relation in JavaScript.

Usage Examples

That.js provides support for the that keyword, which refers to an arbitrarily selected object from anywhere in the global namespace, with the exception of whatever this would refer to in the same context. To get started with that.js, simply link to the library and use that as required:

				function thisAndThat() {
    console.log(this);
    console.log(that);
}

thisAndThat();

// Output:

// Window {window: Window, self: Window, document: document, name: '', location: Location, ...}
// ƒ reportError() { [native code] }
				
			

that may be used in all of the same scenarios as its counterpart this. In the example below, where this denotes an instance of the Example class, that is guaranteed not to refer to this, though the object to which it refers is otherwise indeterminate. Therefore, to avoid a TypeError when setting properties on that, it is recommended to always check explicitly for null and undefined values:

				class Example {
    constructor() {
        this.x = "some property value";
        if (that !== null && that !== undefined) {
    	    that.y = "another property value";
        }
        else {
            throw new Error("Could not set property y: that is null or undefined");
        }
    }
}

const x = new Example();

// Result:
// Example {x: 'some property value'}
				
			

Owing to its inherent indeterminacy, it's usually recommended not to try calling or constructing that—doing so is something of a lottery since, on the balance of probability, it's very unlikely that that will refer to either a function or a constructor. Whether this risk is justified by the opportunity for deadpan error messages like "that is not a function" is, however, a decision left to the developer.

Lastly, arbitrary objects in the global namespace may be accessed via the that keyword without throwing constant errors, via a simple try/catch pattern whereby that is retrieved repeatedly until it happens to refer to the object desired:

function logThat() {
    while (true) {
        try {
            that.log("That");
            break;
        }
        catch {
            console.log("No, not THAT!");
        }
    }
}

// Output:
// No, not THAT! (x 937)
// That!
			

Note that this method of object retrieval has a maximum time complexity of O(∞), and should therefore be avoided in performance-critical situations.