SquirrelFish 似乎错过了 "bind()",如何在没有它的情况下将 JS 回调绑定到 "this"?
SquirrelFish seems to miss "bind()", how to bind a JS callback to "this" without it?
有人知道在没有 "bind()" 的情况下将 JS 回调绑定到 "this" 吗?
2013 在 V8 上:工作正常(参见 linked shot,太大而无法添加)
2012 在 SquirrelFish 上:获取异常:"bind()" 不可作为函数使用(参见 linked shot,太大而无法添加)
违规代码是这样的:
var extJsCallBacks = [
function(){this._setupSystemInfo();}.bind(this),
function(){this._setupConfigInfo();}.bind(this),
function(){this._setupRepository();}.bind(this),
function(){this._setupContents();}.bind(this)
];
有什么提示吗?
Polyfill 在 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
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
if (this.prototype) {
// Function.prototype doesn't have a prototype property
fNOP.prototype = this.prototype;
}
fBound.prototype = new fNOP();
return fBound;
};
}
有人知道在没有 "bind()" 的情况下将 JS 回调绑定到 "this" 吗?
2013 在 V8 上:工作正常(参见 linked shot,太大而无法添加)
2012 在 SquirrelFish 上:获取异常:"bind()" 不可作为函数使用(参见 linked shot,太大而无法添加)
违规代码是这样的:
var extJsCallBacks = [
function(){this._setupSystemInfo();}.bind(this),
function(){this._setupConfigInfo();}.bind(this),
function(){this._setupRepository();}.bind(this),
function(){this._setupContents();}.bind(this)
];
有什么提示吗?
Polyfill 在 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
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
if (this.prototype) {
// Function.prototype doesn't have a prototype property
fNOP.prototype = this.prototype;
}
fBound.prototype = new fNOP();
return fBound;
};
}