i'm creating NumericStepper and Slider component objects. both have very similar properties, except for a few. i'd like to refactor the code for creating them, but i'm not sure about the best way of doing so.
var numericStepper:NumericStepper = new NumericStepper();
numericStepper.width = 50;
numericStepper.minimum = 1;
numericStepper.maximum = 500;
numericStepper.value = 12;
numericStepper.move(20, 20);
numericStepper.addEventListener(Event.CHANGE, changeValueHandler);
var slider:Slider = new Slider();
slider.width = 200;
slider.minimum = 0.0;
slider.maximum = 1.0;
slider.value = 1.0;
slider.liveDragging = true;
slider.snapInterval = 0.01;
slider.move(60, 20);
slider.addEventListener(Event.CHANGE, changeValueHandler);
i'd like to create a function that returns the object, but i don't think i can return a *.
for example:
var numericStepper:NumericStepper = createValueChanger(50, 1, 500, 12, 20, 20);
var slider:Slider = createValueChanger(200, 0.0, 1.0, 1.0, 60, 20);
function createValueChanger(width:Number, min:Number, max:Number, val:Number, x:Number, y:Number):*
{
//create either a NumericStepper or a Slider
}
any ideas for the best approach here?
----------------------- UPDATE -----------------------
i've decided to use the following solution to remedy this problem:
var numericStepper:NumericStepper = new NumericStepper();
var slider:Slider = new Slider();
slider.liveDragging = true;
slider.snapInterval = 0.01;
assignComponentProperties(numericStepper, 50, 1, 500, 12, 20, 20);
assignComponentProperties(slider, 200, 0.0, 1.0, 1.0, 80, 20);
function assignComponentProperties(target:Object, width:Number, min:Number, max:Number, val:Number, x:Number, y:Number):void
{
target.width = width;
target.minimum = min;
target.maximum = max;
target.value = 88;
target.x = x;
target.y = y;
target.addEventListener(Event.CHANGE, changeValueHandler);
}