gpt4 book ai didi

javascript - Mootools 类 protected 处理程序?

转载 作者:行者123 更新时间:2023-11-28 02:44:58 26 4
gpt4 key购买 nike

作为一名 Flash 开发人员,我尝试拥有与 AS3 提供的 mootools 相同的灵 active 。

我尝试做一件简单的事情,创建一个 protected 事件处理函数。我讨厌写内联函数,所以我写了这样的东西:

//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() 可以达到这个目标吗?

非常感谢!

最佳答案

当然可以。您遇到了绑定(bind)错误,而不是 protected 问题

mousedown:function (e){
this.sliderDownHandler();
//throw an error because sliderDownHandler is set to protected
}

不。它抛出一个错误,因为 this 将绑定(bind)到 this.options.slider,它触发了该事件 - 我猜这是一个没有 sliderDownHandler< 的元素 方法。在 protected 方法上遇到的异常是非常独特的,并且不会误会 - 通过在 instance.sliderDownHandler() 上外部调用它来尝试它

重写为以下之一:

var self = this;
...
mousedown:function (e){
self.sliderDownHandler();
}

// or, bind the event to the class instance method...
mousedown: this.sliderDownloadHandler.bind(this)

关于javascript - Mootools 类 protected 处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12052557/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com