Skip to content

Instantly share code, notes, and snippets.

@daviferreira
Created October 14, 2014 19:40
Show Gist options
  • Save daviferreira/5f22f2ba2a07e7bb8531 to your computer and use it in GitHub Desktop.
Save daviferreira/5f22f2ba2a07e7bb8531 to your computer and use it in GitHub Desktop.
react with phantomjs
if (!Function.prototype.bind) {
var Empty = function(){};
Function.prototype.bind = function bind(that) { // .length is 1
var target = this;
if (typeof target != "function") {
throw new TypeError("Function.prototype.bind called on incompatible " + target);
}
var args = Array.prototype.slice.call(arguments, 1); // for normal call
var binder = function () {
if (this instanceof bound) {
var result = target.apply(
this,
args.concat(Array.prototype.slice.call(arguments))
);
if (Object(result) === result) {
return result;
}
return this;
} else {
return target.apply(
that,
args.concat(Array.prototype.slice.call(arguments))
);
}
};
var boundLength = Math.max(0, target.length - args.length);
var boundArgs = [];
for (var i = 0; i < boundLength; i++) {
boundArgs.push("$" + i);
}
var bound = Function("binder", "return function(" + boundArgs.join(",") + "){return binder.apply(this,arguments)}")(binder);
if (target.prototype) {
Empty.prototype = target.prototype;
bound.prototype = new Empty();
// Clean up dangling references.
Empty.prototype = null;
}
return bound;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment