LocalConnection 按值传递还是按引用传递?
使用 LocalConnection 时,两个 SWF 可以相互通信。对象是按值传递还是按引用传递?
When using LocalConnection that have two SWFs talking back and forth to each other. Are objects passed by value or reference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过 LocalConnection 对象传递的数据由发送方序列化为文件,然后接收方将其反序列化,这意味着它会构建一个新对象。
我认为使用的序列化协议是 AMF(如果您使用
registerClassAlias
映射您的类,您的对象将以您传递的相同类型接收;如果不这样做,您将Object< /代码> 对象)。
因此,在接收 swf 时,您将获得对象的新副本。另外,请记住,如果您确实映射类,则必须有一个无参数构造函数或仅采用可选参数的构造函数;否则,当接收方反序列化它们时,您的对象将抛出错误。
The data passed through a LocalConnection object is serialized to a file by the sender and then the receiver deserializes it, meaning it builds a new object.
The serialization protocol used is AMF, I think (if you map your class using
registerClassAlias
your objects will be received with the same type you passed; if you don't, you'llObject
objects).So, in the receiving swf you'll get a fresh copy of your object. Also, keep in mind that if you do map your classes, you must have a parameterless constructor or a constructor that only takes optional parameters; otherwise, your objects will throw an Error when the receiving party deserializes them.
首先,swf 之间的通信必须按值传递(实际参数被完全评估,并将结果值复制到用于保存形式参数值的位置)而不是引用。通过引用传递将违反每个 swf 所在的沙箱。 (因为 localconnection 可以在多个 swf 文件之间使用)。
无论如何,我有兴趣知道你为什么问?您是否正在检查是否需要防御性复制。 (这就是为什么我决定自己挖掘这些信息。)
First of all communication between swf has to be pass by value (The actual parameter is fully evaluated and the resulting value is copied into a location being used to hold the formal parameter's value ) and not reference. Pass by reference will violate the sandbox in which each swf resides. ( since localconnection could be used between multiple swf files).
Anyways I am interested to know why do you ask? Are you checking if you need defensive copying. (that is why I decided to dig in this information myself.)