Search code examples
apache-flexactionscript-3adobebyref

Passing in variables ByRef in Actionscript 3


Is it possible to pass a parameter to a method ByRef (or out etc) in ActionScript 3?

I have some globally scoped variables at the top of my class and my method will populate that variable if it's == null.

I'm passing in the variable which needs to be populated but so far my efforts have returned a locally populated variable leaving the globally scoped version of it still null.

The variable being passed to my method varies so I can't hardcode it in my method and simply set it.


Solution

  • ActionScript 3 passes params by reference by default, like Java - except for primitive types. But what you are trying to have it do isn't passing by reference. The parameter passed in is a reference to an object(in the case when it's not a primitive type), which you may well modify inside of the function.

    But, to answer your question. Here is a solution:

    function populateIfNull(variableName, value){
        this[variableName] = this[variableName] || value
    }
    

    Which you can use like:

    populateIfNull('name', 'Bob')
    populateIfNull('age', 20)