开发者

Why is not permited to access a member function from my local event handler in ActionScript (Flex4)?

开发者 https://www.devze.com 2023-01-10 02:41 出处:网络
My code looks like : private function createExportButton() : void { var exportButton : Button = new Button();

My code looks like :

private function createExportButton() : void {
        var exportButton : Button = new Button();
        exportButton.label = "Export";
        exportButton.x = 6开发者_如何转开发00;
        exportButton.y = 10;
        exportButton.addEventListener(MouseEvent.CLICK, function
                                           clickHandler(e : MouseEvent) : void {
            this.export();
        });
        super.addElement(exportButton);
    }

The error is:

* TypeError: Error #1006: export is not a function. at Function/()[C:\Users\User\Adobe Flash Builder 4\agriculturalAdministration\src\certificate\one\CertificateBuilder1.as:150] * Can anyone explain me why this? Thanks advance!


clickHandler() is not a member function, it does not belong to any class. It is an anonymous function. Therefore, it does not belong to an instance.

However, if I recall correctly, you should be able to refer to variables in the enclosing scope (such as exportButton) from within. In that case, you may want to assign the class reference to a variable and use that instead of this:

var exportButton:Button = new Button();
var this_:SomeClass = this;
...
addEventListener(..., function clickHandler(e:MouseEvent):void {
    this_.export();
});

Or, you can just make clickHandler() a member function:

private function clickHandler(e:MouseEvent):void
{
    this.export();
}

...
{
    addEventListener(..., clickHandler);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消