如何处理IE 8中缺少JavaScript Object.bind()方法
How to handle lack of JavaScript Object.bind() method in IE 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);
};
由于我使用 Firefox 在 WinXP 中开发,有时使用 IE 7 或 10 在 Win10 中进行测试,我没有注意到或注意 IE8 及更低版本不支持 bind
的事实。
这个特定的脚本不使用画布,所以我有点犹豫要不要注销所有IE 8用户。
是否有标准的解决方法?
我在 JavaScript 中走来走去还不错,但我仍然有点菜鸟。因此,如果解决方案完全明显,请原谅我。
此页面上有一个很好的兼容性脚本: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;
};
}
最好的解决方案可能是安装Modernizr。
Modernizr 告诉您当前浏览器是否本机实现了此功能它提供了一个脚本加载器,因此您可以在旧浏览器中引入 polyfill 以回填功能。
以下是生成现代化自定义版本的链接:
http://modernizr.com/download/#-teststyles-testprop-testallprops-hasevent-prefixes-domprefixes
Function.prototype.bind 在 Internet Explorer 8 及更低版本中不受支持。此处的兼容性图表:http://kangax.github.io/es5-compat-table/
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;
};
}
函数构造函数是执行此操作的老式方法:
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) );
引用
- 函数
- 式 JavaScript:利用函数对象的强大功能
- eval() 不是邪恶的,只是被误解了
- 通过函数构造函数创建的函数的 [范围]
- John Resig - Fast JavaScript max/min
- Javascript:函数bind.click&对于每个复选框
- JavaScript:如何将“.bind()”“函数”到另一个对象
- bind函数在javascript中不起作用
- Javascript:正在使用Jquery's bind()比简单地执行onclick='bla()
- ECMAScript 5之前的Javascript-bind()/如何使用apply()或call()进行模拟
- Javascript bind() vs createDelegate()
- Javascript - IE8 alternative to bind()
- javascript knockout data-bind
- 是否有一个名为 bind 的 JavaScript 或 jQuery 事件
- JavaScript 的 bind() with currying.这些代码如何工作
- Javascript bind "this" to event
- 推理,JavaScript bind()函数的用例
- Javascript: bind参数,但保留原始参数
- 为什么javascript bind()不自己绑定属性?
- 使用javascript .bind()方法跳过一个参数
- 我可以用JavaScript bind()访问事件信息吗?
- jQuery .on()数据参数与使用javascript bind()的区别
- JavaScript bind event
- javascript.bind能提升多个级别吗
- ecmascript 6 - Javascript.bind - ES5 Vs ES6