你能有“ByRef”吗? AS3 函数中的参数?

发布于 2024-07-09 03:10:49 字数 433 浏览 10 评论 0原文

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

任何类似 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

除了返回一个关联数组之外还有什么吗?

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

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

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

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"}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

迷迭香的记忆 2024-07-16 03:10:49

引用谷歌搜索的来源:

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

这促使我查找规范来源

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.

Which led me to look up the canonical source.

骑趴 2024-07-16 03:10:49

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

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

var b:String = "old";

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

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

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

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?

二智少女 2024-07-16 03:10:49

除了 [u]int 之外,AS3 中的所有内容都是引用。 概括而言,继承 Object 的所有内容都将通过引用提供给函数。

话虽这么说,我相信您可以做到这一点的唯一方法是使用容器类,如数组或字符串(“5”并进行转换+数学)。

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.

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).

北笙凉宸 2024-07-16 03:10:49

这都是按值计算的,如果您了解 C 编程,您就会熟悉指针的概念。

将指针视为指向内存中的某些内容,所有变量名称“bob from (bob = new person();)”本质上都是您使用的指针。

现在,当您声明一个函数时,由于它们都是按值的,

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

您可以将“a”和“b”视为新指针,因此只有在“Test”函数中,“a”和“b”才存在并指向到记忆中的某件事。

所以让我们使用它

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

所以 s1 和 s2 指针将始终分别指向“null”和“内存中的新 Sprite”,除非它们在其“范围”内被修改为 s1 和 s2 <- 请确保您了解变量范围在尝试解决这个问题之前。

在函数中,我们现在有两个新指针“a”指向“null”,“b”指向“内存中与 s2 相同的精灵”。 因此,由于对象和数组本质上是指针的集合,并且函数仅创建了两个新指针以供使用“a”和“b”,因此“a”或“b”的任何属性/公开变量“指向内存中数据的指针”仍然与“s1”和“s2”完全相同,并且是完全相同的指针。

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

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

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

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.

Now, when you declare a function, since they are all 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.

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.

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.

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.

!!!!
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".

女中豪杰 2024-07-16 03:10:49

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

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);
}

,这是跟踪结果:

null
[object Sprite]

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]
百合的盛世恋 2024-07-16 03:10:49

请注意 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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文