我真的很想能够使Flash的数组访问语法'包裹'在数组的边界。
I'd really like to be able to make Flash's array access syntax 'wrap' over the array's bounds.
冗长的解释 -
var array:Array = ['a','b','c','d','e','f'];
为了简单起见,第一个指数为0,其值的第一个字母A。为了获得该值,我们会做到这一点 -
To keep things simple, the first index is 0, and its value is the first letter, 'a'. To get that value, we'd do this -
array[0]; // returns 'a'
只要你使用访问数组是介于0和array.length指数(6在我们的例子,)一切工作正常 - 但如果你使用这些范围之外的指标,你关机
As long as the index you're using to access the array is between 0 and array.length (6 in our example,) everything works fine - but if you use an index outside of those bounds, you're shut down.
array[-3];
array[9]; // both return 'undefined'
有时候这是一件好事 - 有时你希望这样的事情发生了,你没事吧。其他时候,你发现自己希望(或者至少是我发现自己希望),它会表现得有点多这样的 -
Sometimes that's a good thing - sometimes you expect that to happen, and you're fine with it. Other times, you find yourself wishing (or at least I find myself wishing) that it'd behave a bit more like this -
array[-3];
array[9]; // both return 'd'
(如照相馆的跳回刚开始的时候,你点击的最后一张照片'下一步')
(e.g. a photo gallery that jumps back to the beginning when you click 'next' on the last photo)
还有的code我用了个遍了这样的事情一点大块,但它总是来改变指数的在的它传递到阵列中:
There's a little chunk of code I use over and over for this sort of thing, but it's always to alter the index before passing it into the array:
var index = -3;
while(index < 0){index += array.length}
array[index % array.length]; // returns 'd'
...这很好,但我真正想要做的是延长了数组对象本身,以便它自动会认为出界'包裹'的索引值。
... and that's fine, but what I really want to do is extend the Array object itself so that it'll automatically 'wrap' index values that go out of bounds.
TL; DR - 通过扩展的Flash AS3的数组对象是指数包装可能
TL;DR - Is index-wrapping possible by extending Flash AS3's Array object?
退房Proxy类:http://livedocs.adobe.com/flex/2/langref/flash/utils/Proxy.html.
我没有用它自己,但现在看来,这可以做的工作。我修改了样品code,在文档和它的作品,你想要的方式。我还没有彻底测试它,虽然,你可能会想这样做。就个人而言,我不会扩大阵列,只是做一个简单的类有2个用于添加/检索,因为代理想法似乎有点麻烦我。但是,这是我的。
I haven't used it myself but it seems it could do the job. I modified the sample code in the docs and it works the way you want. I haven't thoroughly tested it, though, and you might want to do it. Personally, I would not extend Array and just make a simple class with 2 methods for adding/retrieving, since the proxy idea seems a bit involved to me. But that's me.
package
{
import flash.utils.Proxy;
import flash.utils.flash_proxy;
dynamic class ProxyArray extends Proxy {
private var _item:Array;
public function ProxyArray() {
_item = new Array();
}
override flash_proxy function callProperty(methodName:*, ... args):* {
var res:*;
res = _item[methodName].apply(_item, args);
return res;
}
override flash_proxy function getProperty(name:*):* {
if(!isNaN(name)) {
var index:int = name;
while(index < 0) {
index += this.length;
}
return _item[index % this.length];
}
return _item[name];
}
override flash_proxy function setProperty(name:*, value:*):void {
_item[name] = value;
}
}
}
使用:
var a:ProxyArray = new ProxyArray();
// you can't use this syntax ['a','b','c'], since it would return an Array object
a.push("a");
a.push("b");
a.push("c");
a.push("d");
a.push("e");
a.push("f");
trace(a[-3]);
trace(a[9]);