:: Forum >>

Adding an Array property to a prototype

I need to add an array to every visible object that can be created as well as a few functions that interact with the array. I thought I had it but it turns out the array is a shared array between all objects that are created. When I try to create the array in the create constructor, the functions that interact with it all start to report that "this.arrayname is null or does not exist". This sounded simple when I started but I am just not getting past this little thing. This is what I had done that almost worked:

Active.System.HTML.prototype.myArray = new Array();
Active.System.HTML.prototype.addToArray = function(newItem){ this.myArray.push(newItem);}
Active.System.HTML.prototype.removefromArray = function(removeItem) { delete this.myArray[this.myArray.ondexOf(removeItem)]

If I created a new object and did obj.addToArray("some item"), then I got obj.myArray.length = 1. But if I create a new object, say obj2, and immediately test it's array length, I get obj2.myArray.length=1 and I obviously want it to be 0.

Any pointers would be helpfull. I assume I am going at this at the wrong level, someone will know.

Jim Hunter
Thursday, August 11, 2005
I found a post that could give you some hints about it:
If i am not completely wrong, the poin is Class-Heirarchy,
Subclassing and creating properties (estatic or function-dinamic) plus some Attibutes, and adding the bennefit of setter/getter (to make set/getProperty calls) is how "the creature's father -- ;-) " ... and Mr/Ms gbegley with this sample do real Magic.
But I am still finding secrets so....... lot to learn

Thursday, August 11, 2005
Thanks for the pointer but I had already read this post and it's not quite what I am doing. This post is about creating a new component via subclassing, I need to inject a few new properties and methods into the existing controls.

Thanks for the assistance.
Jim Hunter
Thursday, August 11, 2005

I can suggest several ways to solve this:

1. Instead of using an array property obj.items use special naming convention for the array elements and make them properties of the parent object – obj.item1, obj.item2 etc. (this is how aw implements HTML styles, attributes and classes collections.

2. override init() method and create a copy of a prototype array (or just new array) every time the new object is created. Don’t forget to call superclass init().

3. same as 2 but only copy (create new) array on element write. Reading elements would use an existing array even if it belongs to the prototype. Don’t use the array directly, make it ‘private’ and create two getItem/setItem methods. Add $owner property to the array which points back to the current object. Inside setItem method check $owner property and if it does not point to the current object – than the array likely belongs to the prototype and should be copied/cloned before adding item to it. If you create new array set $owner property to the current object.

I was using all of three methods at different times so not sure which one is the best, depends on the context.
Alex (ActiveWidgets)
Tuesday, August 16, 2005
Great ideas, I'll see which work best for me. The reason I wanted to use an array was so that if I want to delete an obj I can then loop through all of it's children and delete them as well. If I make them properties of an obj then I will not have a clean way to itterate through them to delete them.

Thanks a ton!
Jim Hunter
Tuesday, August 16, 2005

This topic is archived.

Back to support forum

Forum search