Skip to content

Instantly share code, notes, and snippets.

@pxwise
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pxwise/69a67f504249746718b7 to your computer and use it in GitHub Desktop.
Save pxwise/69a67f504249746718b7 to your computer and use it in GitHub Desktop.
Casperjs bind polyfill
/**
* CasperJS .bind()
*
* Adds .bind() capability missing from PhantomJS < 2.0.0 for CasperJS.
* Needed for some types of evaluations on the remote page as well as
* screenshots.
*/
casper.on('page.initialized', function() {
casper.evaluate(function() {
var isFunction = function(o) {
return typeof o == 'function';
};
var bind;
var slice = [].slice;
var proto = Function.prototype;
var featureMap;
featureMap = {
'function-bind': 'bind'
};
function has(feature) {
var prop = featureMap[feature];
return isFunction(proto[prop]);
}
if (!has('function-bind')) {
bind = function bind(obj) {
var args = slice.call(arguments, 1);
var self = this;
var nop = function() {};
var bound = function() {
return self.apply(this instanceof nop ? this : (obj || {}), args.concat(slice.call(arguments)));
};
nop.prototype = this.prototype || {};
bound.prototype = new nop();
return bound;
};
proto.bind = bind;
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment