如何处理 IE 8 中缺少 JavaScript Object.bind() 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11054511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle lack of JavaScript Object.bind() method in IE 8
提问by Claude
I am writing a bit of JavaScript that uses the Object.bind
method.
我正在编写一些使用该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);
};
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
.
由于我在WinXP下用Firefox开发,有时在Win7下用IE 9或10进行测试,我没有注意到或注意IE8及以下不支持bind
.
This particular script does not use the canvas, so I'm a little hesitant to write off all IE 8 users.
这个特定的脚本不使用画布,所以我有点犹豫要不要注销所有 IE 8 用户。
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.
我在 JavaScript 方面做得还不错,但我仍然有点菜鸟。所以如果解决方案是完全显而易见的,请原谅我。
回答by alexwells
There is a good compatability script on this page: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
这个页面有一个很好的兼容性脚本: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;
};
}
回答by antonjs
The best solution could be to install Modernizr.
最好的解决方案可能是安装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.
Modernizr 会告诉您当前浏览器是否本地实现了此功能,并且它提供了一个脚本加载器,因此您可以在旧浏览器中引入 polyfill 来回填功能。
Here is the link to generate your modernizr custom version:
http://modernizr.com/download/#-teststyles-testprop-testallprops-hasevent-prefixes-domprefixes
这是生成您的 Modernizr 自定义版本的链接:http:
//modernizr.com/download/#-teststyles-testprop-testallprops-hasevent-prefixes-domprefixes
回答by Karl Glennon
Function.prototype.bind isn't supported in Internet Explorer 8 and below. Compatibility chart here: http://kangax.github.io/es5-compat-table/
Internet Explorer 8 及以下版本不支持 Function.prototype.bind。这里的兼容性图表:http: //kangax.github.io/es5-compat-table/
Mozilla Developer Network provide this alternative for older browsers that don't implemented .bind() natively:
Mozilla 开发者网络为未在本地实现 .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;
};
}
回答by Paul Sweatte
The Function constructor is the old-fashioned way of doing this:
Function 构造函数是执行此操作的老式方法:
var foo = function(x,y,z){ return Function("x,y,z","return Math.max.call(this, x, y, z)")(x,y,z) }
var bar = function(x,y,z){ return Function("x,y,z","return Math.min.call(this, x, y, z)")(x,y,z) }
console.log(foo(1,2,3) );
console.log(bar(3,2,1) );
References
参考