一个变量的变化传播到另一个变量 [英] Changes to one variable propagates to another

查看:153
本文介绍了一个变量的变化传播到另一个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有两个ArrayCollection的firstAC和secondAC。如果我做第二个AC = firstAC,并且比第二个AC(也许放置过滤功能)更改它以某种方式传播到第一个AC,谁会告诉我为什么会发生在Flex或ActionScript 3?



如果我只希望secondAC从firstAC获取所有数据,但是当我对secondAC进行更改时,不能在firstAC中显示,我该怎么办? b
$ b

感谢一堆答案!
Ladislav

解决方案

在ECMAScript语言(AS1-3,JavaScript等) p>

  var foo = //一些不是字符串或数字的值
pre>

你真正说的是foo现在指向与其他变量相同的对象。这意味着在这种情况下,两个数组都是相同的值:

$ $ $ $ $ $ $ $ $ var foo:Array = [1,2,3 ]。
foo = bar;
bar.push(4);
trace(foo); // 1,2,3,4

这也适用于函数:

  var foo:Array = [1,2,3]; 
加法器(foo);
函数加法器(bar:Array):void {
bar.push(4);
}

trace(foo); // 1,2,3,4

它甚至可以用于XML:

  var xml:XML =< root>< foo />< / root>; 
var bar:XML = xml;
bar.children()[0]。@ bar = 1;
trace(xml.toXMLString()); //< root>< foo bar =1/>< / root>

这被称为通过引用传递而不是传值或通过复制 。这意味着每次引用一个项目时,每个变量都会指向同一个对象。



有很多方法可以解决这个问题,而且大多数依赖于你的上下文。对于数组,我最喜欢的是Array.concat(),它返回数组的文字克隆。这意味着我对返回值做的任何事情都不会以任何方式影响原文。但是,如果我正在处理XML,我将执行如下操作: var xml2:XML = XML(xml.toXMLString());



在你的情况下,我实际上建议你使用:

$ p $ var $ secondAC:ArrayCollection = new ArrayCollection(firstAC.source.concat());

这样做的好处不仅在于速度更快(它依赖于编译代码而不是Flex SDK代码而且它也不首先实例化一个新的数组然后重新填充它),但是它也具有在旧版本的Flex 3的SDK中可用的明显好处 - 它完全是向后兼容的。


For example I have two ArrayCollection's - firstAC and secondAC. If I do secondAC = firstAC, and than I make changes to secondAC (prehaps put a filterfunction on it) it somehow propagates to firstAC, would anyone tell me why that happens in Flex or Actionscript 3?

What can I do if I only want secondAC to get all data from firstAC but then when I make changes to secondAC it does not show in firstAC?

Thanxs a bunch for answers! Ladislav

解决方案

In ECMAScript languages (AS1-3, JavaScript, et al.), when you use

var foo = //some value which is not a String or a Number

what you are really saying is "foo now points to the same object as that other variable." This means that in this situation, both arrays will be the same value:

var foo:Array = [ 1, 2, 3 ];
foo = bar;
bar.push( 4 );
trace( foo ); // 1, 2, 3, 4

This also works for functions:

var foo:Array = [ 1, 2, 3 ];
adder( foo );
function adder( bar:Array ):void {
    bar.push( 4 );
}

trace( foo ); // 1, 2, 3, 4

and it even works with XML:

var xml:XML = <root><foo/></root>;
var bar:XML = xml;
bar.children()[ 0 ].@bar = 1;
trace( xml.toXMLString() ); // <root><foo bar="1"/></root>

This is called "passing by reference" instead of "passing by value" or "passing by copy". It means that every time that an item is referenced, each variable will point to the same object.

There are many ways to get around this, and most of them depend on your context. For arrays, my favorite is Array.concat(), which returns a literal clone of the array. This means that anything I do to the returned value will not effect the original in any way. If I'm dealing with XML, however, I will do something like: var xml2:XML = XML( xml.toXMLString() );.

In your case, I would actually recommend that you use:

var secondAC:ArrayCollection = new ArrayCollection( firstAC.source.concat() );

This has the major benefits of not only being faster (it relies on compiled code instead of Flex SDK code and it also does not first instantiate a new array and then re-populate it), but it also has the distinct benefit of being available in older versions of Flex 3's SDK -- it is entirely backwards compatible.

这篇关于一个变量的变化传播到另一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆