JSFiddle - React, Tailwind, and code Playground

by James Allardice

JavaScript

var patch = (function () {
    /*jshint evil: true */
    
    "use strict";
    
    var global = new Function("return this;")(), // Get a reference to the global object
        fnProps = Object.getOwnPropertyNames(Function); // Get the own ("static") properties of the Function constructor

    return function (original, originalRef, patched) {
      
        var ref = global[originalRef] = original, // Maintain a reference to the original constructor as a new property on the global object
            args = [],
            newRef; // This will be the new patched constructor
      
        args.length = original.length; // Match the arity of the original constructor
        args.push(" \
            'use strict'; \
            var newed = !!this; \
            return (" + patched + ").apply(null, arguments); \
        "); // This string is evaluated to create the patched constructor body
        newRef = new (Function.prototype.bind.apply(Function, [{}].concat(args)))(); // Create a new function to wrap the patched constructor
        newRef.prototype = original.prototype;
      
        Object.getOwnPropertyNames(ref).forEach(function (property) { // Add any "static" properties of the original constructor to the patched one
            if (fnProps.indexOf(property) < 0) { // Don't include static properties of Function since the patched constructor will already have them
                newRef[property] = ref[property];
            }
        });
      
        return newRef; // Return the patched constructor
    };

}());

console.log(String(10));
console.log(String(false));

String = patch(String, "StringOriginal", function () {
    var args = [].slice.call(arguments),
        strObj;
    if (newed) {
        if (typeof args[0] === "boolean") {
            args[0] = "Patched!";
        }
     // strObj = new (Function.prototype.bind.apply(StringOriginal, [{}].concat(args)))();
      strObj = new StringOriginal(args[0]);
      return strObj;
    }
    return...