| Package | flash.utils |
| Class | public class Proxy |
| Inheritance | Proxy Object |
The Proxy class has no constructor, and you should not attempt to instantiate Proxy.
Instead, subclass the Proxy class to override methods such as
getProperty and provide custom behavior. If you try to use a method of
the Proxy class without overriding the method, an exception is thrown.
The Proxy class is a replacement for the
Object.__resolve and Object.addProperty
features of ActionScript 2.0, which are no longer
available in ActionScript 3.0. The Object.addProperty() feature allowed you to
dynamically create get and set methods in ActionScript 2.0. Although ActionScript 3.0
provides get and set methods at compile time, you cannot dynamically assign one
to an object unless you use the Proxy class.
To avoid collisions with the public namespace,
the methods of the Proxy class are in the
flash_proxy namespace.
Where methods of the Proxy class take a name
argument, name can be either a String or
a QName object (if namespaces are being used).
| Function | Defined by | ||
|---|---|---|---|
|
Overrides the
[[CallProperty]] behavior of
the object. | Proxy | ||
|
Overrides the
[[DeleteProperty]]
behavior of the object. | Proxy | ||
|
Overrides the E4X descendants operator.
| Proxy | ||
|
Overrides the
[[Get]] behavior of
the object. | Proxy | ||
![]() |
Indicates whether an object has a specified property defined.
| Object | |
|
Overrides the
[[HasProperty]] behavior
of the object. | Proxy | ||
|
Returns
true if name is a QName that is also
marked as an attribute. | Proxy | ||
![]() |
Indicates whether an instance of the Object class is in the prototype chain of the object specified
as the parameter.
| Object | |
|
Allows enumeration of the proxied object's properties by index number to
retrieve property names.
| Proxy | ||
|
Allows enumeration of the proxied object's properties by index number.
| Proxy | ||
|
Allows enumeration of the proxied object's properties by index number to
retrieve property values.
| Proxy | ||
![]() |
Indicates whether the specified property exists and is enumerable.
| Object | |
|
Overrides the
[[Put]] behavior of
the object. | Proxy | ||
![]() |
Sets the availability of a dynamic property for loop operations.
| Object | |
![]() |
Returns the string representation of the specified object.
| Object | |
![]() |
Returns the primitive value of the specified object.
| Object | |
flash_proxy function callProperty(name:*, ... rest):*
[[CallProperty]] behavior of
the object. When a method of the object is invoked,
this method is called. The [[CallProperty]] behavior is similar to the [[Call]] behavior as
defined in the ECMA-262 Language Specification, 3rd Edition, section 15. While [[Call]]
behavior means you have an object that is "callable" as a function, [[CallProperty]]
behavior means you have an object with properties that are "callable" as functions.
Parameters
name:* — The name of the method being invoked.
|
|
... rest — An array specifying the arguments to the
called method.
|
* — The return value of the called method.
|
| ECMA-262 Language Specification, 3rd Edition |
flash_proxy function deleteProperty(name:*):Boolean
[[DeleteProperty]]
behavior of the object. When a property is deleted
with the delete operator, this
method is called to perform the deletion. The [[DeleteProperty]] behavior is similar to the
[[Delete]] behavior as
defined in the ECMA-262 Language Specification, 3rd Edition, section 8.6.2.5.
Parameters
name:* — The name of the property to delete.
|
Boolean —
If the property was deleted, true; otherwise false.
|
| ECMA-262 Language Specification, 3rd Edition |
flash_proxy function getDescendants(name:*):*
name:* — The name of the property to descend
into the object and search for.
|
* — The results of the descendants operator.
|
| E4X Specification |
flash_proxy function getProperty(name:*):*
[[Get]] behavior of
the object. Any request for a property's value
calls this method. For more information about [[Get]] behavior, see
the ECMA-262 Language Specification, 3rd Edition, section 8.6.2.1.
Parameters
name:* — The name of the property to retrieve.
|
* — The specified property.
|
| ECMA-262 Language Specification, 3rd Edition |
flash_proxy function hasProperty(name:*):Boolean
[[HasProperty]] behavior
of the object. When ActionScript
checks whether the object has a particular property
by name, this method is invoked to do the test. For more information about [[HasProperty]] behavior, see
the ECMA-262 Language Specification, 3rd Edition, section 8.6.2.4.
Parameters
name:* — The name of the property to check for.
|
Boolean —
If the property exists, true; otherwise false.
|
| ECMA-262 Language Specification, 3rd Edition |
flash_proxy function isAttribute(name:*):Boolean
true if name is a QName that is also
marked as an attribute.
Parameters
name:* — The name of the property to check.
|
Boolean |
flash_proxy function nextName(index:int):String
for...in and
for each..in loops on the object to retrieve the desired names.
For example (with code from Proxy.nextNameIndex()):
protected var a:Array; // array of object's properties
override flash_proxy function nextNameIndex (index:int):int {
if (index == 0) // initial call {
a = new Array();
for (var x in target) {
a.push (x);
}
}
if (index < a.length) {
return index + 1;
}
else
return 0;
}
override flash_proxy function nextName(index:int):String {
return a[index-1];
}
Parameters
index:int — The zero-based index value of the object's property.
|
String —
String The property's name.
|
| Proxy.nextNameIndex(), Proxy.nextValue() |
flash_proxy function nextNameIndex(index:int):int
for...in and
for each..in loops on the object to retrieve property index values.
For example:
protected var a:Array; // array of object's properties
override flash_proxy function nextNameIndex (index:int):int {
if (index == 0) // initial call {
a = new Array();
for (var x in target) {
a.push (x);
}
}
if (index < a.length) {
return index + 1;
}
else
return 0;
}
Parameters
index:int — The zero-based index value where the enumeration begins.
|
int —
The property's index value.
|
| Proxy.nextName(), Proxy.nextValue() |
flash_proxy function nextValue(index:int):*
for...in and
for each..in loops on the object to retrieve the desired values.
For example (with code from Proxy.nextNameIndex()):
protected var a:Array; // array of object's properties
override flash_proxy function nextNameIndex (index:int):int {
if (index == 0) // initial call {
a = new Array();
for (var x in target) {
a.push (x);
}
}
if (index < a.length) {
return index + 1;
}
else
return 0;
}
override flash_proxy function nextValue(index:int):{
return target[a[index-1]];
}
Parameters
index:int — The zero-based index value of the object's property.
|
* — The property's value.
|
| Proxy.nextNameIndex(), Proxy.nextName() |
flash_proxy function setProperty(name:*, value:*):void
[[Put]] behavior of
the object. Any change to a property's value
calls this method. For more information about [[Put]] behavior, see
the ECMA-262 Language Specification, 3rd Edition, section 8.6.2.2.
Parameters
name:* — The name of the property to modify.
|
|
value:* — The value to set the property to.
|
| ECMA-262 Language Specification, 3rd Edition |
package {
import flash.display.Sprite;
public class ProxyExample extends Sprite {
public function ProxyExample() {
var customArr:CustomArray = new CustomArray("one");
customArr.push("two");
customArr.push("three");
customArr.push("four");
trace(customArr.length);// 4
trace(customArr[0]); // one
trace(customArr[1]); // two
trace(customArr[2]); // three
var lengthOfThree:CustomArray;
lengthOfThree = customArr.select(3, function(item:String, value:int){ return item.length == value });
trace(lengthOfThree); // one,two
}
}
}
import flash.errors.IllegalOperationError;
import flash.utils.Proxy;
import flash.utils.flash_proxy;
dynamic class CustomArray extends Proxy {
protected var arr:Array;
protected var props:XML;
public function CustomArray(... args) {
arr = (args.length == 1 && !isNaN(args[0])) ? new Array(args[0]) : args;
props = describeType(arr);
}
public function select(val:int, fn:Function):CustomArray {
var selArr:CustomArray = new CustomArray();
for(var i:uint = 0; i < arr.length; i++) {
if(fn.call(this, arr[i], val)) {
selArr.push(arr[i]);
}
}
return selArr;
}
flash_proxy override function callProperty(methodName:*, ...args):* {
if(props.method.(@name == methodName.toString()).toXMLString() != "") {
return arr[methodName.toString()].apply(null, args);
}
throw new IllegalOperationError("Error: Call to a possibly undefined method " + methodName.toString() + " through a reference with static type CustomArray");
}
flash_proxy override function getProperty(variableName:*):* {
if((props.accessor.(@name == variableName.toString()).toXMLString() != "") || parseInt(String(variableName)) < arr.length) {
return arr[variableName.toString()];
}
throw new IllegalOperationError("Error: Access of undefined property " + variableName.toString() + " through a reference with static type CustomArray");
}
}
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/labs/flashauthoringpreview/flash/utils/Proxy.html