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...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.