JSFiddle - React, Tailwind, and code Playground

by brianarn

HTML

<p>All the action's in the console, go look there.</p>

JavaScript

// Dabbling with augmenting Function in a not so pretty way to make it factory-esque
Function.prototype.new = function() {
    console.log('Creating via new prop!');

    // Create an empty delegate of sorts that will allow us to pick up the prototype
    function Delegate(){}
    Delegate.prototype = this.prototype;
    
    // Create a "new" object using this delegate
    var instance = new Delegate();
    
    // Run this actual function against the new object,
    // using || in the return in case our constructor
    // overrides and returns a value.
    return this.apply(instance, arguments) || instance;
};

// Create our constructor
function Resource(arg){
    console.log('Making a new Resource with arg:', arg);
    this.arg = arg;
}

// Adding something to prototype so we can see something passing through
Resource.prototype.passthru = true;

var foo = new Resource('a');
console.log("foo.arg === 'a'", foo.arg === "a");

var bar = Resource.new('b');
console.log("bar.arg === 'b'", bar.arg === 'b');