JSFiddle - React, Tailwind, and code Playground

by Thomas Upton

JavaScript

function ABC() {
    this.methodA = function() {
        console.log('ABC.methodA', this);
    }.bind(this);
    
    this.methodB = function() {
        console.log('ABC.methodB', this);
        this.methodA();
    }.bind(this);
}

// call static method from instance
var a = new ABC();
a.methodB();

// fails without .bind(this)
b = a.methodB;
b();

function DEF() {}

DEF.prototype.methodA = function() {
    console.log('DEF.methodA', this);
}

DEF.prototype.methodB = function() {
    console.log('DEF.methodB', this);
    this.methodA();
}

// call prototypical method
d = new DEF();
d.methodB();

// also fails without .bind(this)
e = d.methodB;
e();