TommyX
TommyX

Reputation: 93

AS3 functions replacing

I used to work on AS2 and make games, and now I wanna learn AS3 and have all its nice features (using Flash CS IDE). Now I m trying to rewrite a function to discard it.

function something():void{
    //do something
}
function something():void{}

like this. please help or just give some alternatives, thanks.

Upvotes: 0

Views: 161

Answers (1)

Marty
Marty

Reputation: 39458

What you're trying to do is very illogical - a function should be defined once and exist always. Not only that, but it should definitely always behave the same way, especially considering AS3 does not support overloading.

AS3 introduces the OOP paradigm for you to use - this further emphasises the above - you should create classes which define a fixed collection of properties and methods. This way, the intent of each class in your application is clear, and what you expect something to be able to do won't change.

If you absolutely must be able to delete functions, you can assign them to a dynamic object and remove or redefine them with the delete keyword:

var methods:Object = {
    something: function():void
    {
        trace('Still here.');
    }
};


methods.something(); // Still here.
delete methods.something;
methods.something(); // TypeError: something is not a function.

methods.something = function():void
{
    // Define new function.
}

Or assign an anonymous function to a variable of type Function, from which point you can set the reference to null:

var something:Function = function():void
{
    trace("Still here.");
}


something(); // Still here.
something = null;
something(); // TypeError: value is not a function.

something = function():void
{
    // Define new function.
}

Upvotes: 3

Related Questions