User Guide

382 Chapter 6: ActionScript Core Classes
This property is useful for enabling highly transparent client/server communication, and is the
recommended way of invoking server-side methods.
Example
The following examples progressively build upon the first example and illustrate five different
usages of the
__resolve property. To aid understanding, key statements that differ from the
previous usage are in bold typeface.
Usage 1: the following example uses
__resolve to build an object where every undefined
property returns the value
"Hello, world!".
// instantiate a new object
var myObject:Object = new Object();
// define the __resolve function
myObject.__resolve = function (name) {
return "Hello, world!";
};
trace (myObject.property1); // output: Hello, world!
trace (myObject.property2); // output: Hello, world!
Usage 2: the following example uses __resolve as a functor, which is a function that generates
functions. Using
__resolve redirects undefined method calls to a generic function named
myFunction.
// instantiate a new object
var myObject:Object = new Object();
// define a function for __resolve to call
myObject.myFunction = function (name) {
trace("Method " + name + " was called");
};
// define the __resolve function
myObject.__resolve = function (name) {
return function () { this.myFunction(name); };
};
// test __resolve using undefined method names
myObject.someMethod(); // output: Method someMethod was called
myObject.someOtherMethod(); //output: Method someOtherMethod was called
Usage 3: The following example builds on the previous example by adding the ability to cache
resolved methods. By caching methods,
__resolve is called only once for each method of
interest. This allows lazy construction of object methods. Lazy construction is an optimization
technique that defers the creation, or construction, of methods until the time at which a method is
first used.
// instantiate a new object
var myObject:Object = new Object();
// define a function for __resolve to call
myObject.myFunction = function(name) {
trace("Method "+name+" was called");
};