views:

518

answers:

2

Hi

I have an AS class with setter and getter functions. I need to tweak one of this class's instances so that it's setter function will process the input before assigning it to the local variable.

or, in a more elaborated way, what should I use instead of $$$ in the example below?

class MyClass{
 private var _legend:Array;
 function set legend(legend:Array):void{
  _legend= legend;
 }
 function get legend():Array{
  return _legend;
 }
 function someFunction():void{
  foo();
 }
}
var mc:MyClass = new MyClass();
mc.someFunction = function():void{
 bar();
}
mc.$$$ = new function(legend:Array):void{
 _legend = process(legend);
}
A: 

Why don't you pass the instance a processed input?

mc.legend = process(legend);

If this is not possible, you can modify the setter in MyClass and take an optional boolean to do processing.

function set legend(legend:Array, flag:bool = false):void{
            _legend = flag ? process(legend) : legend;
}

Note that prototype inheritance does not restrict itself to a particular instance. From the documentation:

Prototype inheritance - is the only inheritance mechanism in previous versions of ActionScript and serves as an alternate form of inheritance in ActionScript 3.0. Each class has an associated prototype object, and the properties of the prototype object are shared by all instances of the class.

dirkgently
A: 

Normally you would subclass MyClass to modify the behavior (polymorphism) of MyClass.

class MySubClass extends MyClass {
        function set legend(legend:Array):void{
                // do your checking here. Then call the 
                // setter in the super class.

                super.legend = legend;
        }
}
Luke
so, in other words, what i'm looking for is not possible...?
Amir Arad
To me what you're doing looks like hacking on the 'prototype'. Personally I wouldn't do that unless there was no other way. Your problem, as I understand it, can be solved with inheritance.
Luke