flash - In ActionScript, is it possible to call a function on a Class that applies to all instances of that class? -
so - have bunch of instances of class , there's function want call on of them. i'm wondering if rather loop through every instance have, there way can declare function on class when called runs on each instance? example - if class looks this:
public class myclass{ public var variable:string = ""; public function myclass(){} public function myfunction():void{ this.variable = "blore"; } }
and have bunch of these:
var class1:myclass = new myclass(); var class2:myclass = new myclass();
is there way can call myclass.myfunction()
, have called on of instances?
i don't know if i'm explaining well...but there is. i'd love suggestions have don't involve "put instances in array or vector , loop through them real man."
here's quick example of how accomplish this:
package { public class example { public static var instances:array; public function example() { if ( !instances ) { instances = []; } instances.push( ); } public static function setpropertyonall( property:string, value:object ):void { var l:uint = instances.length; ( var i:uint = 0; < l; i++ ) { instances[i][property] = value; } } } }
basically, have static array within class , push each instance of class array upon instantiation. loop through array , change property.
keep in mind few things:
- there no error handling if
property
not exist. sure property pass insetpropertyonall
property or add kind of error check sure. - saving each instance array make them ineligible garbage collection. bad, obviously. if do this, you'll want create way remove each instance array, if has manually done each time. did not because impossible know how/when run or class structure looks like, need figure out. do not want keep them ineligible garbage collection
Comments
Post a Comment