【问题标题】:Mootools class protected handler?Mootools 类受保护的处理程序?
【发布时间】:2012-08-16 15:54:09
【问题描述】:

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

非常感谢!

【问题讨论】:

    标签: javascript class mootools protected


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-03
      • 1970-01-01
      • 2020-04-25
      • 2015-08-04
      • 2013-12-29
      • 2011-08-03
      • 2011-06-06
      相关资源
      最近更新 更多