Where is the watch object in AS3?
The watch object in AS2 was useful to monitor a variable’s change. But it was a problem regarding performances.
With AS3, the watch function disapeared, the object model of AS3 allows to do exactly the same. But not out of the box.
Adobe Livedoc suggests to use the Proxy pattern with setter and getter. Here is a class that does the same as the watch object and is easier to use than the proxy.
package { import flash.events.Event; import flash.events.EventDispatcher; public class Model extends EventDispatcher { public static const VALUE_CHANGED:String = 'value_changed'; private var _number:Number = Number; public function Model():void { trace('The model was instantiated.'); } public function set number(newNb:Number):void { _number=newNb; this.dispatchEvent(new Event(Model.VALUE_CHANGED)); } public function get number():Number { return _number; } } }
The _number variable can be replaced by whatever type is needed.
Usage:
var objectToWatch:Model = new Model(); objectToWatch.addEventListener(Model.VALUE_CHANGED, onValuedChanged); function onValuedChanged(e:Event) { //do what you need here }
Related Posts
No related posts.

I don’t under stand what you have done here completely
more or less
lets say I have a
var cc:int;
I want to watch cc
how can this be done with the code you entered above?
when a certain function is called cc++;
thanks
Is your second dispatched event really useful (the one in the getter) ?
Anyway, it will never be dispatched AFTER the ‘return’ keyword.
Ah ah! thanks! Since this code is online, none saw this! You’re right the second dispatch is useless..not to mention completely useless if after the return..
Thanks!