1

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

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

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

多谢!

4

1 回答 1

1

你当然可以。你有一个绑定错误,不是受保护的问题

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)
于 2012-08-21T15:08:13.803 回答