Skip to main content
April 24, 2012
Question

Analog of Watch() in AS3

  • April 24, 2012
  • 2 replies
  • 3147 views

Hello everyone!

Here is a situation:

var MAIN_OBJ:Object = new Object();

MAIN_OBJ.array_1 = new Array(1, 2, 3);

MAIN_OBJ.array_2 = new Array(A, B, C, D, E, [f,g,h,i,j]);

MAIN_OBJ.array_3 = new Array([k,l,m,n,o,p], Q, R, S, T);

ActionScript2:

function someFunction(prop, oldVal, newVal, limit) { trace("prop is about to change"); return newVal;}

MAIN_OBJ.watch("array_1", someFunction);

ActionScript3:

Use accessor properties (get/set functions) or the flash.utils.Proxy class for similar functionality.

Here is the task:

Watch some property of object to change, and do smth. Watch not the whole object, only one property.

I know that ExternalInterface.addCallback provides smth like that, but for the whole object.

I've never used get/set functions or override smth, please explain what should exactly be written to achieve the same effect as in AS2. The simplier it would be - the better.

Sorry fo my English.

This topic has been closed for replies.

2 replies

Participating Frequently
September 23, 2022

I meet ths same situation,something like javascript's Object.defineProperty.I just want it dispatch auto,so that I can update my pane

kglad
Community Expert
Community Expert
September 23, 2022

if you want to dispatch a js event, use:

 

whatever_element.dispatchEvent(new Event("whatever_event"));

Participating Frequently
September 25, 2022

In fact,I'm going to make my panel update when an object's specify property change.Yes,I can bind a listener and dispacth it after this object's specify change,but the question is I don't want to do the dispatch step,like hide it in somewhere.In ES5 method Object.defineObjectProperty() can help me do this,it use getter() and setter() do something more when  an object' property change,but not a proxy.

So I'm wondering whether if this is a way can achieve some like this in AS3.

kglad
Community Expert
Community Expert
April 25, 2012

// in watching class:

watchedClass.addEventListener("propertyChangedE",propertyChangedF);

private function propertyChangedF(e:Event):void{

// do whatever

}

// in watched class, dispatch event whenever changed from inside the class and from outside:

public function set someProperty(someValue:ValueType):void{

_someProperty=someValue;

dispatchEvent(new Event("propertyChangedE"));

}

April 26, 2012

I suppose that to change someProperty, I should use

setProperty ( target, someProperty , value);

I'd better add more details to the question:

var MAIN_OBJ:Object = new Object();

MAIN_OBJ.array_1 = new Array(1, 2, 3);

MAIN_OBJ.array_2 = new Array(A, B, C, D, E, [f,g,h,i,j]);

MAIN_OBJ.array_3 = new Array([k,l,m,n,o,p], Q, R, S, T);

MAIN_OBJ.array_1.push(4);

MAIN_OBJ.array_1.splice(1,1);

I think, in this case it is not possible to use setProperty 

kglad
Community Expert
Community Expert
April 26, 2012

again, // in watched class, dispatch event whenever changed from inside the class and from outside: