# Dynamically adding properties to class via prototype for AS3

**URL:** https://forum.kirupa.com/t/dynamically-adding-properties-to-class-via-prototype-for-as3/283473
**Category:** flash
**Created:** [March 6, 2009, 4:35pm UTC](https://forum.kirupa.com/t/dynamically-adding-properties-to-class-via-prototype-for-as3/283473 "2009-03-06T16:35:19Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![elliot\_geno](https://avatars.discourse-cdn.com/v4/letter/e/34f0e0/32.png) [@elliot\_geno](https://forum.kirupa.com/u/elliot_geno)
#### Post date: [March 6, 2009, 4:35pm UTC](https://forum.kirupa.com/t/dynamically-adding-properties-to-class-via-prototype-for-as3/283473/1 "2009-03-06T16:35:19Z")

</div>

I tried rewriting Senocular’s AlignmentProperties that alter the prototype of a MovieClip for AS1 and AS2 in AS3… (Basically it adds a top, left, bottom, right, centerY, centerX property to all MovieClips so you can align them regardless of their registration point)

Now, I know you (senocular) say prototypes are not typically used anymore for AS3 but they are still there in case you need them. I think this utility could be pretty useful. What are your thoughts on how a property could be dynamically added to a class in AS3?

The usage would look something like this:  
import AlignmentProperties;  
new AlignmentProperties(“flash.display.MovieClip”);// or some other DisplayObject or special class  
trace(my\_mc.top);// traces the top of a movie clip regardless of the registration  
my\_mc.top=0; //aligns the top of a movieclip to 0 regardless of the registration

But ran into a snag adding properties to a class via prototype.

Here is my class…

```auto
package 
{
	/////////////////////////////////////////////////////////////////////////////////
	/// Written by Elliot Geno >> March 3, 2009
	/// Utility to add alignment properties to a developer-defined class.
	/// Based off an idea presented by Senocular but rewitten for AS3
	///
	/// USAGE:
	/// import AlignmentProperties;
	/// new AlignmentProperties("flash.display.MovieClip");
	/// trace(my_mc.top);//traces the top of any movieclip
	///
	/// import AlignmentProperties;
	/// new AlignmentProperties("com.some.other.SpecialClass");
	/// trace(special_mc.top);//traces the top of any SpecialClass
	///
	/////////////////////////////////////////////////////////////////////////////////
	import flash.events.Event;
	import flash.display.DisplayObject;
	import flash.geom.Rectangle;
	import flash.utils.getDefinitionByName;
	//
	public dynamic class AlignmentProperties extends DisplayObject
	{
		public var ignoreStrokes:Boolean=false; //// !! This is handy to choose whether or not you want to use getRect or getBounds
		public var myObject:DisplayObject;
		//
		public function AlignmentProperties(myClass:String)
		{
			var classRef:Class=getDefinitionByName(myClass) as Class;
			///
			classRef.prototype.top=top;//??How do you add properties to a prototype in AS3???
			classRef.prototype.blah=5;//variables and functions work fine.
		}
		////////////////////////
		// TOP
		public function get top():Number
		{
			var r:Rectangle=getRectangle();
			return r.y + this.y;
		}
		public function set top(value:Number):void
		{
			var r:Rectangle=getRectangle();
			this.y=value-r.y;
		}
		////////////////////////
		// BOTTOM
		public function get bottom():Number
		{
			var r:Rectangle=getRectangle();
			return r.y + this.y + r.height;
		}
		public function set bottom(value:Number):void
		{
			var r:Rectangle=getRectangle();
			this.y=value-r.y-r.height;
		}
		////////////////////////
		// LEFT
		public function get left():Number
		{
			var r:Rectangle=getRectangle();
			return r.x + this.x;
		}
		public function set left(value:Number):void
		{
			var r:Rectangle=getRectangle();
			this.x=value-r.x;
		}
		////////////////////////
		// RIGHT
		public function get right():Number
		{
			var r:Rectangle=getRectangle();
			return r.x + this.x + r.width;
		}
		public function set right(value:Number):void
		{
			var r:Rectangle=getRectangle();
			this.x=value-r.x-r.width;
		}
		////////////////////////
		// CENTER Y
		public function get centerY():Number
		{
			var r:Rectangle=getRectangle();
			return r.y + this.y + r.height * .5;
		}
		public function set centerY(value:Number):void
		{
			var r:Rectangle=getRectangle();
			this.y=(value-(r.height*.5))-r.y;
		}
		////////////////////////
		// CENTER X
		public function get centerX():Number
		{
			var r:Rectangle=getRectangle();
			return r.x + this.x + r.width * .5;
		}
		public function set centerX(value:Number):void
		{
			var r:Rectangle=getRectangle();
			this.x=(value-(r.width*.5))-r.x;
		}
		////////////////////////
		// RETURN RECTANGLE
		private function getRectangle():Rectangle
		{
			var r:Rectangle;
			if (ignoreStrokes) {
				r= this.getRect(this);
			} else {
				r= this.getBounds(this);
			}
			return r;
		}
	}
}

```
