Javascript Mootools类保护处理程序?
作为一名flash开发人员,我尝试使用mootools提供与AS3相同的灵活性 我尝试做一件简单的事情,创建一个受保护的事件处理函数。 我不喜欢写内联函数,所以我写了这样的东西:Javascript Mootools类保护处理程序?,javascript,class,mootools,protected,Javascript,Class,Mootools,Protected,作为一名flash开发人员,我尝试使用mootools提供与AS3相同的灵活性 我尝试做一件简单的事情,创建一个受保护的事件处理函数。 我不喜欢写内联函数,所以我写了这样的东西: //CLASS DEFINITION AS USUAL initializeEvent:function (){ if (this.options.slider) this.options.slider.addEvents ({ mousedown:function (e){
//CLASS DEFINITION AS USUAL
initializeEvent:function (){
if (this.options.slider) this.options.slider.addEvents ({
mousedown:function (e){
this.sliderDownHandler();
//throw an error because sliderDownHandler is set to protected
}
});
},
update:function (){
this.fireEvent('update');
}.protect(),
sliderDownHandler:function (e){
this.update();
console.log ('yeah it down')
}.protect();
如果没有.protect(),处理程序将按预期工作
使用.protected()可以实现此目标
非常感谢 当然可以。您有一个绑定错误,而不是受保护的问题
mousedown:function (e){
this.sliderDownHandler();
//throw an error because sliderDownHandler is set to protected
}
否。它正在引发错误,因为this
将绑定到this.options.slider
,它触发了事件-我猜这是一个没有sliderDownHandler
方法的元素。您在受保护的方法上遇到的异常非常独特,不会弄错-请在instance.sliderDownHandler()
重新编写为以下内容之一:
var self = this;
...
mousedown:function (e){
self.sliderDownHandler();
}
// or, bind the event to the class instance method...
mousedown: this.sliderDownloadHandler.bind(this)
当然可以。您有一个绑定错误,而不是受保护的问题
mousedown:function (e){
this.sliderDownHandler();
//throw an error because sliderDownHandler is set to protected
}
否。它正在引发错误,因为this
将绑定到this.options.slider
,它触发了事件-我猜这是一个没有sliderDownHandler
方法的元素。您在受保护的方法上遇到的异常非常独特,不会弄错-请在instance.sliderDownHandler()
重新编写为以下内容之一:
var self = this;
...
mousedown:function (e){
self.sliderDownHandler();
}
// or, bind the event to the class instance method...
mousedown: this.sliderDownloadHandler.bind(this)