JSFiddle - React, Tailwind, and code Playground
JavaScript
Function.prototype.bind = function (boundThis) {
// closest thing possible to the ECMAScript 5 internal IsCallable function
if (typeof this !== "function") {
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}
var targetFunction = this;
var boundArgs = Array.prototype.slice.call(arguments, 1);
var params = [];
for (var i = 0; i < targetFunction.length - boundArgs.length; i++) {
params.push('_' + i);
}
var F = function () {
var extraArgs = Array.prototype.slice.call(arguments);
var totalArgs = boundArgs.concat(extraArgs);
// F is called as a constructor (cf. 15.3.4.5.2)
if (this instanceof F) { // may yield false positives.
// workaround to use "new" with an arguments array,
// taken from http://stackoverflow.com/q/7667163/27862
var str = 'new targetFunction(';
for (var i = 0; i < totalArgs.length; i++) {
str += (i ? ',' : '') + 'totalArgs[' + i + ']';
}
str += ');';
return eval(str);
} else { // F called as a function (15.3.4.5.1)
return targetFunction.apply(boundThis, totalArgs);
}
};
eval('F = ' + F.toString().replace(/function *\(/, 'function(' + params.join(',')));
F.prototype = targetFunction.prototype;
return F;
};
var DateJan2042 = Date.bind(null, 2042, 0);
/*1*/ console.assert(Function.prototype.bind.length == 1, 'bind should have a length of 1');
/*2*/ console.assert(typeof DateJan2042 == 'function', 'bind() should return a function');
/*3*/ console.assert(!DateJan2042.hasOwnProperty('prototype'), 'Bound function must not have a prototype');
/*4*/ console.assert(DateJan2042.length == Math.max(Date.length - 2, 0), 'Bound function should have a proper length');
/*5*/ console.assert(typeof DateJan2042() == 'string', 'Function call should return a...