我正在编写一些 JavaScript,它使用Object.bind
method.
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);
};
由于我在WinXP中使用Firefox进行开发,有时在Win7中使用IE 9或10进行测试,因此我没有注意到或注意IE8及以下版本不支持bind
.
这个特定的脚本不使用画布,所以我有点犹豫是否要注销所有 IE 8 用户。
有标准的解决方法吗?
我在 JavaScript 方面的表现还不错,但我仍然有点菜鸟。如果解决方案是完全显而易见的,请原谅我。
这个页面有一个很好的兼容性脚本:https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
只需将其复制并粘贴到您的脚本中即可。
EDIT:为了清楚起见,将脚本放在下面。
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;
};
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)