Last active
August 29, 2015 14:20
-
-
Save pxwise/69a67f504249746718b7 to your computer and use it in GitHub Desktop.
Casperjs bind polyfill
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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