我正在做一些跨浏览器测试。 Initially, I was using the bind() keyword in ECMAScript 5。它在IE8中不可用。作为解决方法,我使用了来自Yehuda Katz的一些代码。他的网站提出替代的bind()的使用效用函数时绑定不存在 - 它不会在IE 8如何在IE-8 Javascript的函数体中绑定“this”?
http://yehudakatz.com/2011/08/11/understanding-javascript-function-invocation-and-this/
var bind = function (func, thisValue) {
return function() {
return func.apply(thisValue, arguments);
}
}
存在在尝试使用它,我当它说“func.apply”时会得到一个异常。我没有传入一个函数,我传递了一个对象,所以apply()不在那里。精细。但是现在我又被卡住了,回到方块1.我如何将“this”变量绑定到函数体?
摘要:如何将“this”变量绑定到函数体?
<snip>
MyView.prototype.OnSlider_Change = function (event, ui) {
this.viewModel.setVariableX(ui.value * 100.0);
}
<snip>
$("#Slider").slider({
slide: (this.OnSlider_Change).bind(this),
change: (this.OnSlider_Change).bind(this)
});
已成为
$("#Slider").slider({
slide: bind(this, this.OnSlider_Change),
change: bind(this, this.OnSlider_Change)
});
Yehuda Katz不是JavaScript的发明者;)他是个很酷的人,他非常熟悉JavaScript。 – yatskevich 2012-02-13 17:36:55