重要度:(3/5) 这个问题看似简单,但 debug 时却难以发现。
数组访问时如果下标使用了函数并且使用了 +=、-=、*= 等运算符时需要特别地注意,函数会被调用两次!
比如以下代码:
var i:int = 0;
var foo = function():int {
trace("foo");
return ++i;
}
var arr:Array = [1,2,3];
arr[foo()] += 10;
trace(arr);
将会输出:
foo; foo; 1,13,3
而正确的输出为:
foo; 1,12,3
这是因为在编译时编译器只做了一下简单的操作:
(a += b) --> (a) = (a) + (b)
以至于将 arr[foo()] += 10; 编译成 arr[foo()] = arr[foo()] + 10;。
![[Google]](http://www.google.com/logos/Logo_25wht.gif)
![[Synchronous Space]](http://weihe924stephen.googlepages.com/syncspace-banner.jpg)
![[Creative Commons License]](http://i.creativecommons.org/l/by-nc-sa/3.0/88x31.png)
![[Add to Google]](http://buttons.googlesyndication.com/fusion/add.gif)
![[Aggregated by MXNA]](http://weblogs.macromedia.com/mxna/images/mxna88x31.gif)
![[Flex.org]](http://www.flex.org/images/flexorg.gif)
![Validate my Atom 1.0 feed [Valid Atom 1.0]](http://validator.w3.org/feed/images/valid-atom.png)
![Validate my RSS feed [Valid RSS]](http://resource.googlecode.com/files/valid-rss.png)
![[I heart FeedBurner]](http://www.feedburner.com/fb/images/pub/i_heart_fb.gif)
没有评论:
发表评论