I am writing a bit of JavaScript that uses the Object.bind
method.
funcabc = function(x, y, z){
this.myx = x;
this.playUB = function(w) {
if ( this.myx === null ) {
// do blah blah
return;
}
// do other stuff
};
this.play = this.playUB.bind(this);
};
Since I develop in WinXP with Firefox and sometimes test in Win7 with IE 9 or 10, I did not notice or pay attention to the fact that IE8 and below do not support bind
.
This particular script does not use the canvas, so I'm a little hesitant to write off all IE 8 users.
Is there a standard work-around?
I'm getting around sort of okay in JavaScript, but I'm still a bit of a noob. So forgive me if the solution is totally obvious.
There is a good compatability script on this page: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
Just copy and paste it into your script.
EDIT: placing the script below for clarity.
The best solution could be to install Modernizr.
Modernizr tells you whether the current browser has this feature natively implemented or not and it provides a script loader so you can pull in polyfills to backfill functionality in old browsers.
Here is the link to generate your modernizr custom version:
http://modernizr.com/download/#-teststyles-testprop-testallprops-hasevent-prefixes-domprefixes
Function.prototype.bind isn’t supported in Internet Explorer 8 and below. Compatibility chart here: http://kangax.github.io/es5-compat-table/
Mozilla Developer Network provide this alternative for older browsers that don't implemented .bind() natively:
The Function constructor is the old-fashioned way of doing this:
References