Packageflash.utils
Classpublic class Proxy
InheritanceProxy Inheritance Object

The Proxy class lets you override the default behavior of ActionScript operations (such as retrieving and modifying properties) on an 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).

View the examples.

Public Properties
Hide Inherited Public Properties
Show Inherited Public Properties
 PropertyDefined by
 Inheritedconstructor : Object
A reference to the class object or constructor function for a given object instance.
Object
 Inheritedprototype : Object
[static] A reference to the prototype object of a class or function object.
Object
Public Methods
Hide Inherited Public Methods
Show Inherited Public Methods
 FunctionDefined by
  
callProperty(name:*, ... rest):*
Overrides the [[CallProperty]] behavior of the object.
Proxy
  
Overrides the [[DeleteProperty]] behavior of the object.
Proxy
  
Overrides the E4X descendants operator.
Proxy
  
getProperty(name:*):*
Overrides the [[Get]] behavior of the object.
Proxy
 Inherited
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
 Inherited
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
  
nextValue(index:int):*
Allows enumeration of the proxied object's properties by index number to retrieve property values.
Proxy
 Inherited
Indicates whether the specified property exists and is enumerable.
Object
  
setProperty(name:*, value:*):void
Overrides the [[Put]] behavior of the object.
Proxy
 Inherited
Sets the availability of a dynamic property for loop operations.
Object
 Inherited
Returns the string representation of the specified object.
Object
 Inherited
Returns the primitive value of the specified object.
Object
Method detail
callProperty method

flash_proxy function callProperty(name:*, ... rest):*

Overrides the [[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.

Returns
* — The return value of the called method.

See also
ECMA-262 Language Specification, 3rd Edition
deleteProperty method

flash_proxy function deleteProperty(name:*):Boolean

Overrides the [[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.

Returns
Boolean — If the property was deleted, true; otherwise false.

See also
ECMA-262 Language Specification, 3rd Edition
getDescendants method

flash_proxy function getDescendants(name:*):*

Overrides the E4X descendants operator. When the descendants operator is used, this method is invoked. For more information see the ECMA E4X Specification.

Parameters
name:* — The name of the property to descend into the object and search for.

Returns
* — The results of the descendants operator.

See also
E4X Specification
getProperty method

flash_proxy function getProperty(name:*):*

Overrides the [[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.

Returns
* — The specified property.

See also
ECMA-262 Language Specification, 3rd Edition
hasProperty method

flash_proxy function hasProperty(name:*):Boolean

Overrides the [[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.

Returns
Boolean — If the property exists, true; otherwise false.

See also
ECMA-262 Language Specification, 3rd Edition
isAttribute method

flash_proxy function isAttribute(name:*):Boolean

Returns true if name is a QName that is also marked as an attribute.

Parameters
name:* — The name of the property to check.

Returns
Boolean
nextName method

flash_proxy function nextName(index:int):String

Allows enumeration of the proxied object's properties by index number to retrieve property names. However, you cannot enumerate the properties of the Proxy class themselves. This function supports implementing 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.

Returns
String — String The property's name.

See also
Proxy.nextNameIndex(), Proxy.nextValue()
nextNameIndex method

flash_proxy function nextNameIndex(index:int):int

Allows enumeration of the proxied object's properties by index number. Hhowever, you cannot enumerate the properties of the Proxy class themselves. This function supports implementing 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.

Returns
int — The property's index value.

See also
Proxy.nextName(), Proxy.nextValue()
nextValue method

flash_proxy function nextValue(index:int):*

Allows enumeration of the proxied object's properties by index number to retrieve property values. However, you cannot enumerate the properties of the Proxy class themselves. This function supports implementing 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.

Returns
* — The property's value.

See also
Proxy.nextNameIndex(), Proxy.nextName()
setProperty method

flash_proxy function setProperty(name:*, value:*):void

Overrides the [[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.

See also
ECMA-262 Language Specification, 3rd Edition
Class examples

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