views:

21

answers:

1

Hello

My code looks like :

private function createExportButton() : void {
        var exportButton : Button = new Button();
        exportButton.label = "Export";
        exportButton.x = 600;
        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!

+4  A: 

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);
}
aib
Here is the obligatory +1 that the OP forgot to give you..!
Amarghosh