我有一个具有原始iPad的客户端,我注意到它是不支持.bind方法。
问:如果我的老板坚持支持IOS 5.1.1,那么除了把变量传递给回调之外,还有别的选择吗?我认为我不能简单地将变量放入全局范围,因为如果我在循环中,我设置的变量可能会覆盖回调所要寻找的相同的变量。
发布于 2013-12-02 09:45:14
您可以使用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;
};
}
发布于 2015-01-07 08:30:25
如果您正在使用npm模块,那么只需执行以下操作就可以得到更简单的解决方案。
npm install --save polyfill-function-prototype-bind
和
require('polyfill-function-prototype-bind');
此模块包含与接受的答案相同的多填充代码。
https://www.npmjs.com/package/polyfill-function-prototype-bind
https://stackoverflow.com/questions/20334494
复制