Monday, May 13, 2024
2
rated 0 times [  3] [ 1]  / answers: 1 / hits: 25920  / 12 Years ago, fri, june 15, 2012, 12:00:00

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.


More From » internet-explorer-8

 Answers
10

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.



if (!Function.prototype.bind) {
Function.prototype.bind = function(oThis) {
if (typeof this !== 'function') {
// closest thing possible to the ECMAScript 5
// internal IsCallable function
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
}

var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function() {},
fBound = function() {
return fToBind.apply(this instanceof fNOP && oThis
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};

fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();

return fBound;
};
}

[#84873] Thursday, June 14, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
quinlanhenryf

Total Points: 743
Total Questions: 93
Total Answers: 118

Location: Australia
Member since Sat, May 27, 2023
1 Year ago
;