03.07.2013 Views

Guide de reference du langage ActionScript 2.0 - PowWeb

Guide de reference du langage ActionScript 2.0 - PowWeb

Guide de reference du langage ActionScript 2.0 - PowWeb

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.

<strong>de</strong>fine a function for __resolve to call<br />

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

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

};<br />

// <strong>de</strong>fine the __resolve function<br />

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

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

};<br />

// test __resolve using un<strong>de</strong>fined method names<br />

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

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

Usage 3 : L'exemple suivant développe l'exemple précé<strong>de</strong>nt en permettant <strong>de</strong> placer les<br />

métho<strong>de</strong>s résolues en mémoire cache. En plaçant les métho<strong>de</strong>s en mémoire cache, __resolve<br />

n'est appelé qu'une seule fois pour chaque métho<strong>de</strong> concernée. Ceci autorise la construction<br />

paresseuse <strong>de</strong>s métho<strong>de</strong>s d'objet. La construction paresseuse est une technique d'optimisation<br />

qui reporte la création, ou construction, <strong>de</strong>s métho<strong>de</strong>s jusqu'à la première utilisation d'une<br />

métho<strong>de</strong>.<br />

// instantiate a new object<br />

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

// <strong>de</strong>fine a function for __resolve to call<br />

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

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

};<br />

// <strong>de</strong>fine 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 <strong>reference</strong> to it<br />

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

this.myFunction(name);<br />

};<br />

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

this[name] = f;<br />

// return the <strong>reference</strong><br />

return f;<br />

};<br />

// test __resolve using un<strong>de</strong>fined 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 <strong>de</strong>fined<br />

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

myObject.someOtherMethod(); // does not call __resolve, no longer un<strong>de</strong>fined<br />

Usage 4 : L'exemple suivant développe l'exemple précé<strong>de</strong>nt en réservant un nom <strong>de</strong> métho<strong>de</strong>,<br />

onStatus(), pour l'utilisation locale <strong>de</strong> façon à ce qu'il ne soit pas résolu <strong>de</strong> la même façon<br />

que les autres propriétés non définies. Le co<strong>de</strong> ajouté figure en gras.<br />

Object 1037

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

Saved successfully!

Ooh no, something went wrong!