什么是在Actionscript 3.0中模拟传递引用的最简洁方法?

时间:2022-07-05 22:29:25

Actionscript 3.0 (and I assume Javascript and ECMAScript in general) lacks pass-by-reference for native types like ints. As a result I'm finding getting values back from a function really clunky. What's the normal pattern to work around this?

Actionscript 3.0(我假设Javascript和ECMAScript一般)缺少像int这样的本机类型的传递引用。结果我发现从一个真正笨重的函数中获取值。解决这个问题的正常模式是什么?

For example, is there a clean way to implement swap( intA, intB ) in Actionscript?

例如,是否有一种干净的方法在Actionscript中实现swap(intA,intB)?

8 个解决方案

#1


7  

I Believe the best you can do is pass a container object as an argument to a function and change the values of some properties in that object:

我相信你能做的最好的事情是将容器对象作为参数传递给函数并更改该对象中某些属性的值:

function swapAB(aValuesContainer:Object):void
{
    if (!(aValuesContainer.hasOwnProperty("a") && aValuesContainer.hasOwnProperty("b")))
        throw new ArgumentError("aValuesContainer must have properties a and b");

    var tempValue:int = aValuesContainer["a"];
    aValuesContainer["a"] = aValuesContainer["b"];
    aValuesContainer["b"] = tempValue;
}
var ints:Object = {a:13, b:25};
swapAB(ints);

#2


4  

I suppose an alternative would be somewhere defining this sort of thing ...

我想一个替代方案可以定义这种事情...

public class Reference {
    public var value:*;
}

Then use functions that take some number of Reference arguments to act as "pointers" if you're really just looking for "out" parameters and either initialize them on the way in or not and your swap would become:

然后使用带有一些Reference参数的函数作为“指针”,如果你真的只是在寻找“out”参数并且在进入或不进行初始化它们并且你的交换将成为:

function swap(Reference a, Reference b) {
    var tmp:* = a.value;
    a.value = b.value;
    b.value = tmp;
}

And you could always go nuts and define specific IntReference, StringReference, etc.

你总是可以疯狂地定义特定的IntReference,StringReference等。

#3


4  

This is nitpicking, but int, String, Number and the others are passed by reference, it's just that they are immutable. Of course, the effect is the same as if they were passed by value.

这是挑剔,但int,String,Number和其他通过引用传递,它只是它们是不可变的。当然,效果与通过值传递的效果相同。

#4


3  

You could also use a wrapper instead of int:

您也可以使用包装器而不是int:

public class Integer
{
    public var value:int;

    public function Integer(value:int)
    {
        this.value = value;
    }
}

Of course, this would be more useful if you could use operator overloading...

当然,如果您可以使用运算符重载,这将更有用...

#5


2  

Just look at some Java code. Java has had the convention that reference types are passed by reference and primitive types are passed by value since it's inception. It's a very good model in many ways.

只看一些Java代码。 Java有这样的约定:引用类型是通过引用传递的,而原始类型是通过值传递的,因为它是一开始的。它在很多方面都是一个非常好的模型。

But talking about swap, the best and easiest way to do a swap in Java/AS3 is with the following three lines:

但谈到swap,在Java / AS3中进行交换的最佳和最简单的方法是使用以下三行:

var temp:int = array[i];
array[j] = array[i];
array[i] = temp;

Theres not really any reason to use a function to do a simple swap, when you can do it faster with just 3 lines.

当你只用3行就可以更快地完成它时,没有任何理由使用函数来进行简单的交换。

#6


1  

It is annoying. But if you use different idioms than in e.g. C#, you can get reasonable-quality results. If you need to pass a lot of parameters back and forth, pass in an object filled with the needed data, and change the object's parameters when you return. The Object class is for just this sort of thing.

这很烦人。但是,如果你使用不同的成语,例如C#,您可以获得合理的质量结果。如果需要来回传递大量参数,请传入一个填充了所需数据的对象,并在返回时更改对象的参数。 Object类仅用于此类事情。

If you just need to return a bunch of data, return an Object. This is more in keeping with the ECMAScript style than pass-by-ref semantics.

如果您只需要返回一堆数据,则返回一个Object。这更符合ECMAScript样式而不是pass-by-ref语义。

#7


0  

Destructuring assignment (e.g. [a,b] = [b,a]) isn't defined in the ECMA-262 3 specification, and it's not implemented in JavaScript 1.5, which is the version equivalent to the JScript implementation in IE. I've seen this syntax in the AS4 specifications preview though, and I believe it's part of JavaScript 1.7.

解构分配(例如[a,b] = [b,a])没有在ECMA-262 3规范中定义,并且它没有在JavaScript 1.5中实现,这是与IE中的JScript实现等效的版本。我已经在AS4规范预览中看到了这种语法,我相信它是JavaScript 1.7的一部分。

#8


-2  

If ActionScript works like Javascript,

如果ActionScript像Javascript一样工作,

[a,b] = [b,a]

#1


7  

I Believe the best you can do is pass a container object as an argument to a function and change the values of some properties in that object:

我相信你能做的最好的事情是将容器对象作为参数传递给函数并更改该对象中某些属性的值:

function swapAB(aValuesContainer:Object):void
{
    if (!(aValuesContainer.hasOwnProperty("a") && aValuesContainer.hasOwnProperty("b")))
        throw new ArgumentError("aValuesContainer must have properties a and b");

    var tempValue:int = aValuesContainer["a"];
    aValuesContainer["a"] = aValuesContainer["b"];
    aValuesContainer["b"] = tempValue;
}
var ints:Object = {a:13, b:25};
swapAB(ints);

#2


4  

I suppose an alternative would be somewhere defining this sort of thing ...

我想一个替代方案可以定义这种事情...

public class Reference {
    public var value:*;
}

Then use functions that take some number of Reference arguments to act as "pointers" if you're really just looking for "out" parameters and either initialize them on the way in or not and your swap would become:

然后使用带有一些Reference参数的函数作为“指针”,如果你真的只是在寻找“out”参数并且在进入或不进行初始化它们并且你的交换将成为:

function swap(Reference a, Reference b) {
    var tmp:* = a.value;
    a.value = b.value;
    b.value = tmp;
}

And you could always go nuts and define specific IntReference, StringReference, etc.

你总是可以疯狂地定义特定的IntReference,StringReference等。

#3


4  

This is nitpicking, but int, String, Number and the others are passed by reference, it's just that they are immutable. Of course, the effect is the same as if they were passed by value.

这是挑剔,但int,String,Number和其他通过引用传递,它只是它们是不可变的。当然,效果与通过值传递的效果相同。

#4


3  

You could also use a wrapper instead of int:

您也可以使用包装器而不是int:

public class Integer
{
    public var value:int;

    public function Integer(value:int)
    {
        this.value = value;
    }
}

Of course, this would be more useful if you could use operator overloading...

当然,如果您可以使用运算符重载,这将更有用...

#5


2  

Just look at some Java code. Java has had the convention that reference types are passed by reference and primitive types are passed by value since it's inception. It's a very good model in many ways.

只看一些Java代码。 Java有这样的约定:引用类型是通过引用传递的,而原始类型是通过值传递的,因为它是一开始的。它在很多方面都是一个非常好的模型。

But talking about swap, the best and easiest way to do a swap in Java/AS3 is with the following three lines:

但谈到swap,在Java / AS3中进行交换的最佳和最简单的方法是使用以下三行:

var temp:int = array[i];
array[j] = array[i];
array[i] = temp;

Theres not really any reason to use a function to do a simple swap, when you can do it faster with just 3 lines.

当你只用3行就可以更快地完成它时,没有任何理由使用函数来进行简单的交换。

#6


1  

It is annoying. But if you use different idioms than in e.g. C#, you can get reasonable-quality results. If you need to pass a lot of parameters back and forth, pass in an object filled with the needed data, and change the object's parameters when you return. The Object class is for just this sort of thing.

这很烦人。但是,如果你使用不同的成语,例如C#,您可以获得合理的质量结果。如果需要来回传递大量参数,请传入一个填充了所需数据的对象,并在返回时更改对象的参数。 Object类仅用于此类事情。

If you just need to return a bunch of data, return an Object. This is more in keeping with the ECMAScript style than pass-by-ref semantics.

如果您只需要返回一堆数据,则返回一个Object。这更符合ECMAScript样式而不是pass-by-ref语义。

#7


0  

Destructuring assignment (e.g. [a,b] = [b,a]) isn't defined in the ECMA-262 3 specification, and it's not implemented in JavaScript 1.5, which is the version equivalent to the JScript implementation in IE. I've seen this syntax in the AS4 specifications preview though, and I believe it's part of JavaScript 1.7.

解构分配(例如[a,b] = [b,a])没有在ECMA-262 3规范中定义,并且它没有在JavaScript 1.5中实现,这是与IE中的JScript实现等效的版本。我已经在AS4规范预览中看到了这种语法,我相信它是JavaScript 1.7的一部分。

#8


-2  

If ActionScript works like Javascript,

如果ActionScript像Javascript一样工作,

[a,b] = [b,a]