你能在AS3函数中使用“ByRef”参数吗?

时间:2021-10-04 16:34:53

Any idea how to return multiple variables from a function in ActionScript 3?

知道如何从ActionScript 3中的函数返回多个变量吗?

Anything like VB.NET where you can have the input argument's variable modified (ByRef arguments)?

像VB.NET,你可以修改输入参数的变量(ByRef参数)吗?

Sub do (ByRef inout As Integer)
 inout *= 5;
End Sub

Dim num As Integer = 10
Debug.WriteLine (num)        '10
do (num)
Debug.WriteLine (num)        '50

Anything apart from returning an associative array?

除了返回关联数组之外的任何东西?

return {a:"string 1", b:"string 2"}

6 个解决方案

#1


3  

Everything in AS3 is a reference aside from [u]ints. To generalize, everything that inherits Object will be given to the function by a reference.

除了[u]整数之外,AS3中的所有内容都是参考。为了概括,继承Object的所有内容都将通过引用赋予函数。

That being said, the only way I believe you can do it is use a container class like an Array or a String ("5" and do the conversion+math).

话虽这么说,我相信你能做到的唯一方法是使用像Array或String这样的容器类(“5”并进行转换+数学运算)。

#2


15  

Quoting a googled source:

引用谷歌来源:

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.

在ActionScript 3.0中,所有参数都通过引用传递,因为所有值都存储为对象。但是,属于原始数据类型的对象(包括Boolean,Number,int,uint和String)具有特殊运算符,这些运算符使它们的行为就像它们按值传递一样。

Which led me to look up the canonical source.

这导致我查找规范来源。

#3


6  

It appears that Strings, ints, units, Booleans are passed by Value. I tried this little snippet in Flash and the results were negative:

似乎字符串,整数,单位,布尔值都是通过值传递的。我在Flash中尝试了这个小片段,结果是否定的:

function func(a:String){
    a="newVal";
}

var b:String = "old";

trace(b)    //  old
func(b);
trace(b)    //  old

So... is String a blacklisted data type too? Boolean too? I mean whats a sure way of telling which types are passed by reference?

那么...... String也是列入黑名单的数据类型吗?布尔也呢?我的意思是什么方式告诉哪些类型通过引用传递?

#4


2  

It's all by value, if you understand C programming you will be familiar with the concept of pointers.

这完全取决于价值,如果您了解C编程,您将熟悉指针的概念。

Think about a pointer as pointing to something in memory, and all variable names "bob from (bob = new person();)" Are essentially pointers that you work with.

想想一个指针指向内存中的某个东西,所有变量名称“bob from(bob = new person();)”基本上都是你使用的指针。

Now, when you declare a function, since they are all By Value

现在,当您声明一个函数时,因为它们都是By Value

function Test(a:Object, b:Object):void {
   a = b;
}

You can think about both "a" and "b" being new pointers, so only within the "Test" function do both "a" and "b" exist and point to something in memory.

您可以将“a”和“b”都视为新指针,因此仅在“Test”函数中,“a”和“b”都存在并指向内存中的某些内容。

So let's use it

所以让我们使用它

var s1:Sprite = null;
var s2:Sprite = new Sprite;
Test(s1,s2);

So the s1 and s2 pointers will ALWAYS point to "null" and "a new Sprite in memory" respectively, unless they are modified as s1 and s2 within their "Scope" <- Please make sure you understand variable scope before even trying to tackle this.

所以s1和s2指针总是分别指向“null”和“内存中的新Sprite”,除非它们在“Scope”中被修改为s1和s2 < - 请确保在解决变量范围之前甚至试图解决这个。

And within the function we now have two new pointers "a" pointing to "null" and "b" pointing to "the same sprite in memory as s2". so Since objects and arrays are essentially collections of pointers and only two new pointers have been created by the function for use "a" and "b" any properties/exposed variables "pointers to data in memory" of "a" or "b" will still be exactly the same as the ones for "s1" and "s2" and are the exact same pointers.

在函数中,我们现在有两个新指针“a”指向“null”,“b”指向“内存中与s2相同的sprite”。因为对象和数组本质上是指针的集合,并且函数只创建了两个新指针,用于使用“a”和“b”任何属性/暴露变量“指向内存中数据的指针”的“a”或“b”仍将与“s1”和“s2”完全相同,并且是完全相同的指针。

So within the function when "a" gets set to be "b", really all that happens is the "a" pointer now points to the same thing as "b". But "s1" and "s2" still point to what they were pointing to before.

因此,当“a”被设置为“b”时,在函数内,实际上所发生的一切都是“a”指针现在指向与“b”相同的东西。但是“s1”和“s2”仍然指向他们之前指出的内容。

!!!! If this was by reference you would not be able to think of "a" and "b" as new pointers, they would actually be "s1" and "s2" themselves, except you write them out as "a" and "b".

!!!!如果这是通过引用你将无法将“a”和“b”视为新指针,它们实际上将是“s1”和“s2”本身,除非你把它们写成“a”和“b” 。

#5


2  

Wrong Wrong Wrong and Wrong.. every Argument is passed by value!!! the fact you can change a property inside the object passed doesn't mean you can change the object itself. try the following code

错误错误和错误..每个参数都是通过价值传递的!您可以更改传递的对象内的属性这一事实并不意味着您可以更改对象本身。尝试以下代码

function Test(a:Object, b:Object):void {
   a = b;
}

function Test2():void {
   var s1:Sprite = null;
   var s2:Sprite = new Sprite;

   Test(s1,s2);
   Trace(s1);
   Trace(s2);
}

and here's the trace result :

这是跟踪结果:

null
[object Sprite]

#6


0  

Note the subtle difference between DarthZorG's example and this one from the Flash docs:

请注意DarthZorG的示例与Flash文档中的这个示例之间的细微差别:

function passByRef(objParam:Object):void 
{ 
    objParam.x++; 
    objParam.y++; 
    trace(objParam.x, objParam.y); 
} 
var objVar:Object = {x:10, y:15}; 
trace(objVar.x, objVar.y); // 10 15 
passByRef(objVar); // 11 16 
trace(objVar.x, objVar.y); // 11 16

Point Being: You can't change what the reference is pointing to but you can change the data that the reference is pointing to, so long as that reference is an Object/Array.

点存在:您无法更改引用指向的内容,但您可以更改引用指向的数据,只要该引用是对象/数组即可。

#1


3  

Everything in AS3 is a reference aside from [u]ints. To generalize, everything that inherits Object will be given to the function by a reference.

除了[u]整数之外,AS3中的所有内容都是参考。为了概括,继承Object的所有内容都将通过引用赋予函数。

That being said, the only way I believe you can do it is use a container class like an Array or a String ("5" and do the conversion+math).

话虽这么说,我相信你能做到的唯一方法是使用像Array或String这样的容器类(“5”并进行转换+数学运算)。

#2


15  

Quoting a googled source:

引用谷歌来源:

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.

在ActionScript 3.0中,所有参数都通过引用传递,因为所有值都存储为对象。但是,属于原始数据类型的对象(包括Boolean,Number,int,uint和String)具有特殊运算符,这些运算符使它们的行为就像它们按值传递一样。

Which led me to look up the canonical source.

这导致我查找规范来源。

#3


6  

It appears that Strings, ints, units, Booleans are passed by Value. I tried this little snippet in Flash and the results were negative:

似乎字符串,整数,单位,布尔值都是通过值传递的。我在Flash中尝试了这个小片段,结果是否定的:

function func(a:String){
    a="newVal";
}

var b:String = "old";

trace(b)    //  old
func(b);
trace(b)    //  old

So... is String a blacklisted data type too? Boolean too? I mean whats a sure way of telling which types are passed by reference?

那么...... String也是列入黑名单的数据类型吗?布尔也呢?我的意思是什么方式告诉哪些类型通过引用传递?

#4


2  

It's all by value, if you understand C programming you will be familiar with the concept of pointers.

这完全取决于价值,如果您了解C编程,您将熟悉指针的概念。

Think about a pointer as pointing to something in memory, and all variable names "bob from (bob = new person();)" Are essentially pointers that you work with.

想想一个指针指向内存中的某个东西,所有变量名称“bob from(bob = new person();)”基本上都是你使用的指针。

Now, when you declare a function, since they are all By Value

现在,当您声明一个函数时,因为它们都是By Value

function Test(a:Object, b:Object):void {
   a = b;
}

You can think about both "a" and "b" being new pointers, so only within the "Test" function do both "a" and "b" exist and point to something in memory.

您可以将“a”和“b”都视为新指针,因此仅在“Test”函数中,“a”和“b”都存在并指向内存中的某些内容。

So let's use it

所以让我们使用它

var s1:Sprite = null;
var s2:Sprite = new Sprite;
Test(s1,s2);

So the s1 and s2 pointers will ALWAYS point to "null" and "a new Sprite in memory" respectively, unless they are modified as s1 and s2 within their "Scope" <- Please make sure you understand variable scope before even trying to tackle this.

所以s1和s2指针总是分别指向“null”和“内存中的新Sprite”,除非它们在“Scope”中被修改为s1和s2 < - 请确保在解决变量范围之前甚至试图解决这个。

And within the function we now have two new pointers "a" pointing to "null" and "b" pointing to "the same sprite in memory as s2". so Since objects and arrays are essentially collections of pointers and only two new pointers have been created by the function for use "a" and "b" any properties/exposed variables "pointers to data in memory" of "a" or "b" will still be exactly the same as the ones for "s1" and "s2" and are the exact same pointers.

在函数中,我们现在有两个新指针“a”指向“null”,“b”指向“内存中与s2相同的sprite”。因为对象和数组本质上是指针的集合,并且函数只创建了两个新指针,用于使用“a”和“b”任何属性/暴露变量“指向内存中数据的指针”的“a”或“b”仍将与“s1”和“s2”完全相同,并且是完全相同的指针。

So within the function when "a" gets set to be "b", really all that happens is the "a" pointer now points to the same thing as "b". But "s1" and "s2" still point to what they were pointing to before.

因此,当“a”被设置为“b”时,在函数内,实际上所发生的一切都是“a”指针现在指向与“b”相同的东西。但是“s1”和“s2”仍然指向他们之前指出的内容。

!!!! If this was by reference you would not be able to think of "a" and "b" as new pointers, they would actually be "s1" and "s2" themselves, except you write them out as "a" and "b".

!!!!如果这是通过引用你将无法将“a”和“b”视为新指针,它们实际上将是“s1”和“s2”本身,除非你把它们写成“a”和“b” 。

#5


2  

Wrong Wrong Wrong and Wrong.. every Argument is passed by value!!! the fact you can change a property inside the object passed doesn't mean you can change the object itself. try the following code

错误错误和错误..每个参数都是通过价值传递的!您可以更改传递的对象内的属性这一事实并不意味着您可以更改对象本身。尝试以下代码

function Test(a:Object, b:Object):void {
   a = b;
}

function Test2():void {
   var s1:Sprite = null;
   var s2:Sprite = new Sprite;

   Test(s1,s2);
   Trace(s1);
   Trace(s2);
}

and here's the trace result :

这是跟踪结果:

null
[object Sprite]

#6


0  

Note the subtle difference between DarthZorG's example and this one from the Flash docs:

请注意DarthZorG的示例与Flash文档中的这个示例之间的细微差别:

function passByRef(objParam:Object):void 
{ 
    objParam.x++; 
    objParam.y++; 
    trace(objParam.x, objParam.y); 
} 
var objVar:Object = {x:10, y:15}; 
trace(objVar.x, objVar.y); // 10 15 
passByRef(objVar); // 11 16 
trace(objVar.x, objVar.y); // 11 16

Point Being: You can't change what the reference is pointing to but you can change the data that the reference is pointing to, so long as that reference is an Object/Array.

点存在:您无法更改引用指向的内容,但您可以更改引用指向的数据,只要该引用是对象/数组即可。