應用
關於 Proxy 類的另一個問題,以及為什麼它不那麼受歡迎,是很難理解一個完全需要動態類的問題,並且可以動態訪問其動態屬性和方法作為最合適的解決方案。每當我嘗試使用代理時,我最終都會採用其他方式,更簡單,更可控。
但是,我們不要氣餒。我喜歡用 Python 中的[-1],[ - 2]等索引來定址最後一個陣列元素的想法。可能不是一個偉大的壯舉,但使用它感覺很好,而不是漫長而笨拙的 someArray [someArray.length - 1] 。讓我們看看我們能做些什麼。
package
{
import flash.utils.Proxy;
import flash.utils.flash_proxy;
/**
* Pyaray the Tentacled Whisperer of Impossible Secrets.
*/
dynamic public class PyArray extends Proxy
{
private var data:Array;
public function PyArray(...args:Array)
{
if (args.length == 0)
{
data = new Array;
}
else if ((args.length == 1) && (args[0] is Array))
{
data = args[0];
}
else
{
data = args;
}
}
// This is a getter proxy to all the available Array
// elements and properties and, sometimes, methods.
override flash_proxy function getProperty(name:*):*
{
var anIndex:int = name;
// Handle the int indices of the Array elements.
if (anIndex == name)
{
// Handle the -1, -2, etc indexing.
if (anIndex < 0) anIndex += data.length;
if (anIndex >= data.length) return null;
if (anIndex < 0) return null;
return data[anIndex];
}
// Handle the existing public Array properties.
if (data.hasOwnProperty(name)) return data[name];
// Handle the Array methods addressed via ["member"] access.
try
{
if (data[name] is Function) return data[name];
else throw new Error;
}
catch (fail:Error)
{
trace("[PyArray] is unable to resolve property \"" + name + "\".");
}
return null;
}
// This will set either elements, or settable properties.
override flash_proxy function setProperty(name:*, value:*):void
{
var anIndex:int = name;
// Handle the int indices of the Array elements.
if (anIndex == name)
{
// Handle the -1, -2, etc indexing.
if (anIndex < 0) anIndex += data.length;
// In case the element index is out of range,
// the PyArray will extend its data Array.
// if (anIndex >= data.length) return;
if (anIndex < 0) return;
data[anIndex] = value;
return;
}
// Handle the existing (or dynamic) public Array properties.
try
{
data[name] = value;
}
catch (fail:Error)
{
trace("[PyArray] is unable to set property \"" + name + "\".");
}
return;
}
// This allows to delete PyArray elements with "delete" operator.
override flash_proxy function deleteProperty(name:*):Boolean
{
var anIndex:int = name;
// Handle the int indices of the Array elements.
if (anIndex == name)
{
// Handle the -1, -2, etc indexing.
if (anIndex < 0) anIndex += data.length;
if (anIndex >= data.length) return false;
if (anIndex < 0) return false;
data.splice(anIndex, 1);
return true;
}
// Handle the dynamic public Array properties.
try
{
delete data[name];
return true;
}
catch (fail:Error)
{
trace("[PyArray] is unable to delete the \"" + name + "\" property.");
}
return false;
}
// This proxies any attempt to call a method on PyArray directly to data Array, thus
// all Array methods (including "toString" method called through trace) are available.
override flash_proxy function callProperty(name:*, ...rest):*
{
try
{
return (data[name] as Function).apply(data, rest);
}
catch (fail:Error)
{
trace("[PyArray] is unable to resolve method \"" + name + "\".");
return null;
}
}
// This allows PyArray to handle for..in and for..each..in loops.
// The initial call starts with zero, so we need to do this +1 -1 magic
// in order for enumeration to work correctly. I'm not happy with this either.
override flash_proxy function nextNameIndex(index:int):int
{
if (index >= data.length) return 0;
else return index + 1;
}
// This method handles the for..in loop.
override flash_proxy function nextName(index:int):String
{
return (index - 1).toString();
}
// This method handles the for..each..in loop.
override flash_proxy function nextValue(index:int):*
{
return data[index - 1];
}
}
}