Cla*_*ude 24 javascript bind cross-browser backwards-compatibility internet-explorer-8
我正在编写一些使用该Object.bind
方法的JavaScript .
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);
};
Run Code Online (Sandbox Code Playgroud)
由于我使用Firefox开发WinXP,有时使用IE 9或10在Win7中进行测试,因此我没有注意到或注意IE8及以下版本不支持的事实bind
.
这个特殊的脚本不使用画布,所以我有点犹豫要不要注销所有的IE 8用户.
有标准的解决方法吗?
我在JavaScript中有点好,但我仍然有点像菜鸟.如果解决方案完全明显,请原谅我.
ale*_*lls 49
此页面上有一个很好的兼容性脚本:https: //developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
只需将其复制并粘贴到您的脚本中即可.
编辑:为了清楚起见,将脚本放在下面.
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;
};
}
Run Code Online (Sandbox Code Playgroud)