PDA

View Full Version : Extending Array


Gillespie
11-10-2003, 07:05 AM
Is there any way to extend the Array Class with extra methods in AS 2.0, so that it will function like a prototype in AS 1.0?

I'm trying, in a different Class, to invoke a Method for an array, but can't seem to get it to work.

i.e. in that different Class, I write

myArray.inArray(string);

and I have, as the first line in my 'mothercode' import Array2, which is a Class I've made that has the inArray Method.

It works fine with


Array.prototype.inArray=function(value):Boolean{
var z=this.length;
for(var a=0;a<z;a++){
if(this[a]==value)return true;
};
return false;
};


but I really want to learn how to do this in a way that will conform to AS 2.0, so that I won't be left with something that might be archaic later on.

Thanks for any and all replies.

hangalot
11-10-2003, 08:14 AM
you would not actually be working on the prototype of the class in AS2 rather you would extendd that class through inheritance and add your desired method. thus you would not instantiate an array class but your derived class.

black
11-10-2003, 10:56 AM
til now there are 2 ways to reach your destination.

1. AS1 way~
as you mentioned you can go prototype in Flash MX 2004 and as you guessed it is allowed.

2. AS2 way~
and we should give this way a formal name: inheritance. ie, we could expand built-in classes with inheritance by using "extends" keyword. just as other OOP languages the subclass inherits all properties and method of the super class and could have properties and methods of its own.

hope this helps~

Gillespie
11-10-2003, 07:54 PM
I found out what to do from
blog-o-fobik Extending AS2 Array (http://www.klaustrofobik.org/blog/archives/000198.html).
Thanks alot to both of you for the help.

ain
10-24-2007, 06:31 PM
Array.inArray() method in ActionScript2 (http://tekkie.flashbit.net/flash/as/arrayinarray-method-in-actionscript2)