JSFiddle - React, Tailwind, and code Playground

by robertjd

HTML

This demonstrates how "this" is affected by how you call a function

JavaScript

(function() {
    var Collection = function Collection() {
        this.id = 1234;
    }
    Collection.prototype.handleIncoming = function handleIncoming(message) {
        console.log("Incoming message", message);
        console.log("And 'this.id' is", this.id);
    }

    var Transport = function Transport() {
        this.id = 9876;
    }
    Transport.prototype.receiver = function receiver(reciever) {
        this.receiver = reciever
    }
    Transport.prototype.handler = function handler(handler) {
        this.handler = handler
    }
    Transport.prototype.incoming = function incoming(message) {
        this.receiver.handleIncoming(message);
        this.handler(message);
        this.handler.call(this.receiver, message);
    }

    var c = new Collection();
    var t = new Transport();
    t.receiver(c)
    t.handler(c.handleIncoming)
    t.incoming("Test1")

})()