tomish
tomish

Reputation: 77

AS3 variable declared as a null function

I have encountered an AS3 function that is declared as a null variable, as in:

public var edgeWeights:Function = null;

I am not sure how to use this function to change null to another value (e.g., a number like 2 or 3). I thought something like cs.edgeWeights = 2 might work, but that creates a compile error, as does cs.edgeWeights(2);

I believe these are anonymous functions in AS3 and I did do some research on them, but could not find a resolution to this situation.

Upvotes: 0

Views: 784

Answers (3)

Iagows
Iagows

Reputation: 1099

Given any function:

public function someFunction()
{
    ...
}

You can create a "pointer" with this: this.edgeWeights = someFunction; (yes, without ())

Later you just use: this.edgeWeights(); and you'll be calling someFunction().

Upvotes: 0

Amy Blankenship
Amy Blankenship

Reputation: 6961

If you assume that the Class that declares edgeWeights is Widget:

protected var widget:Widget;

protected function createWidget():void {
    widget = new Widget();
    widget.edgeWeights = widgetCallback;
}

//signature would need to match what the Widget
//actually expects this callback to do
protected function widgetCallback():void {
    trace('hi from widget callback');
}

Note that it's probably bad practice to have a public callback variable and not provide a default implementation, so if you have access to the source code, you should probably fix that.

Upvotes: 0

Johnny Moonlight
Johnny Moonlight

Reputation: 128

public var edgeWeights:Function = null;

This notation means declaring variable edgeWeights of type Function. In Actionscript Function is an object and can be set to null.

To use it you need to set this variable to some function. For example: edgeWeights = function(a:int,b:int):int { return a+b } or edgeWeights = Math.sin.

What function you should set there depends on your particular case.

Upvotes: 3

Related Questions