如何处理缺少JavaScript Object.bind()方法在IE 8 [英] How to handle lack of JavaScript Object.bind() method in IE 8
问题描述
我写了一些使用 Object.bind
方法的JavaScript。
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);
};
由于我在WinXP中使用Firefox开发,有时在Win9中使用IE 9或10进行测试,所以我没有通知或注意IE8及以下版本不支持 bind
。
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
.
这个特定的脚本不使用画布,所以我有点犹豫,注销所有IE 8用户。
This particular script does not use the canvas, so I'm a little hesitant to write off all IE 8 users.
有标准的解决方法吗?
我还是有点noob。如果解决方案是完全明显的,请原谅我。
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.
推荐答案
此页面上有一个很好的兼容性脚本:
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
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.
编辑 >
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;
};
}
这篇关于如何处理缺少JavaScript Object.bind()方法在IE 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!