我有一个原始iPad的客户端,我注意到它是doesn't support the .bind method。
问:如果我的老板坚持支持IOS 5.1.1,是否有将变量传递给回调的替代方法?我不认为我可以简单地将变量放入全局范围,因为如果我在循环中,我设置的变量可能会覆盖回调所需的同一变量。答案 0 :(得分:9)
您可以使用MDN提供的实现,甚至是您自己的实现。
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;
};
}
答案 1 :(得分:0)
如果您使用的是npm模块,只需执行
即可获得更简单的解决方案npm install --save polyfill-function-prototype-bind
和
require('polyfill-function-prototype-bind');
此模块包含与接受的答案相同的填充代码。
https://www.npmjs.com/package/polyfill-function-prototype-bind