JSFiddle - React, Tailwind, and code Playground

by Evilzebra

JavaScript

"use strict";

function Base(x) {
    var that = this;
    this.sub = new (function Sub(_this) {
        this.method = (function(_this) {
           return function() {
                return _this.x;
           }; // return an anonymous function that uses the outer x, but for reasons the anonymous function can't take parameters
        })(_this);
    })(this);
}


function Other() {
    Base.call(this); // why do I need to call Base.call(this) ?
    this.setX = function(x) {this.x = x;}
}
Other.prototype = new Base();
var other = new Other();

other.setX(5);
console.log(other.sub.method()); // otherwise undefined?