参数是否存储为局部变量?

Are parameters stored as local variables?

这里有一个非常简单的问题:在内存分配方面,参数是否被视为局部变量?

以这两个函数为例:

function foo(parameter:Number):void
    {
        trace("Parameter =", parameter);
    }

    function bar():void
    {
        var x:Number;
        trace("x is a number", x is Number);
    }

A​​ctionScript 是否以相同的方式处理 parameterx?它们是否在每次函数 运行 时都被创建为局部变量,并且将一直存在直到 GC 摆脱它们,或者参数被区别对待?

Does ActionScript handle both parameter and x in the same way? Are they both created as local variables each time the function is run, and will remain in existence until GC gets rid of them, or are parameters treated differently?

运行时处理参数的方式与局部变量略有不同,但最终它们都是局部作用域,并且在函数 returns 时被清除。对于所有意图和目的,参数是局部变量。

重要的是要理解how arguments passed to functions work in AS3

In ActionScript 3.0, all arguments are passed by reference, because all values are stored as objects. However, objects that belong to the primitive data types, which includes Boolean, Number, int, uint, and String, have special operators that make them behave as if they were passed by value.

All other objects—that is, objects that do not belong to the primitive data types—are always passed by reference, which gives you ability to change the value of the original variable.

换句话说:

function test(primitive:int, complex:Array):void {
    primitive = 1;
    complex.push("foo");
}

var i:int = 0;
var a:Array = [];
test(i, a);
trace(i); // 0
trace(a); // ["hi"]