你能有“ByRef”吗? AS3 函数中的参数?
知道如何从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
引用谷歌搜索的来源:
这促使我查找规范来源。
Quoting a googled source:
Which led me to look up the canonical source.
看起来字符串、整数、单位、布尔值都是通过值传递的。
我在 Flash 中尝试了这个小片段,结果是否定的:
那么... String 也是列入黑名单的数据类型吗? 也是布尔值吗?
我的意思是有什么可靠的方法来告诉哪些类型是通过引用传递的?
It appears that Strings, ints, units, Booleans are passed by Value.
I tried this little snippet in Flash and the results were negative:
So... is String a blacklisted data type too? Boolean too?
I mean whats a sure way of telling which types are passed by reference?
除了 [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 aString
("5" and do the conversion+math).这都是按值计算的,如果您了解 C 编程,您就会熟悉指针的概念。
将指针视为指向内存中的某些内容,所有变量名称“bob from (bob = new person();)”本质上都是您使用的指针。
现在,当您声明一个函数时,由于它们都是按值的,
您可以将“a”和“b”视为新指针,因此只有在“Test”函数中,“a”和“b”才存在并指向到记忆中的某件事。
所以让我们使用它
所以 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
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
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".
错误错误错误错误..每个参数都是按值传递的!
您可以更改传递的对象内部的属性这一事实并不意味着您可以更改对象本身。 尝试以下代码
,这是跟踪结果:
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
and here's the trace result :
请注意 DarthZorG 的示例与 Flash 文档中的示例之间的细微差别:
要点:
您无法更改引用所指向的内容,但可以更改引用所指向的数据,只要该引用是对象/数组即可。
Note the subtle difference between DarthZorG's example and this one from the Flash docs:
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.