03.05.2013 Views

ActionScript 2.0 Language Reference - Adobe Help and Support

ActionScript 2.0 Language Reference - Adobe Help and Support

ActionScript 2.0 Language Reference - Adobe Help and Support

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

define the __resolve function<br />

myObject.__resolve = function (name) {<br />

return function () { this.myFunction(name); };<br />

};<br />

// test __resolve using undefined method names<br />

myObject.someMethod(); // output: Method someMethod was called<br />

myObject.someOtherMethod(); //output: Method someOtherMethod was called<br />

Usage 3: The following example builds on the previous example by adding the ability to cache<br />

resolved methods. By caching methods, __resolve is called only once for each method of<br />

interest. This allows lazy construction of object methods. Lazy construction is an optimization<br />

technique that defers the creation, or construction, of methods until the time at which a<br />

method is first used.<br />

// instantiate a new object<br />

var myObject:Object = new Object();<br />

// define a function for __resolve to call<br />

myObject.myFunction = function(name) {<br />

trace("Method "+name+" was called");<br />

};<br />

// define the __resolve function<br />

myObject.__resolve = function(name) {<br />

trace("Resolve called for "+name); // to check when __resolve is called<br />

// Not only call the function, but also save a reference to it<br />

var f:Function = function () {<br />

this.myFunction(name);<br />

};<br />

// create a new object method <strong>and</strong> assign it the reference<br />

this[name] = f;<br />

// return the reference<br />

return f;<br />

};<br />

// test __resolve using undefined method names<br />

// __resolve will only be called once for each method name<br />

myObject.someMethod(); // calls __resolve<br />

myObject.someMethod(); // does not call __resolve because it is now defined<br />

myObject.someOtherMethod(); // calls __resolve<br />

myObject.someOtherMethod(); // does not call __resolve, no longer undefined<br />

Usage 4: The following example builds on the previous example by reserving a method name,<br />

onStatus(), for local use so that it is not resolved in the same way as other undefined<br />

properties. Added code is in bold typeface.<br />

// instantiate a new object<br />

var myObject:Object = new Object();<br />

// define a function for __resolve to call<br />

myObject.myFunction = function(name) {<br />

trace("Method "+name+" was called");<br />

};<br />

1016 <strong>ActionScript</strong> classes

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!