views:

304

answers:

1

I'm making a Flex project, using the Cairngorm library, and trying to keep the code in a good MVC structure. I just added some code to add a prototype function to a built-in class (I added the method "contains" to Array), and I'm wondering what you would consider the best-practice for where to put this code in my project structure?

+1  A: 

In my honest opinion, it would be best to subclass or compose on Array, instead of modifying its prototype. Modifying the prototype can lead to confusion in the maintenance phase, which is one of the main reasons for using a framework like Cairngorm.

If creating a new class doesn't suit you, then also consider creating a utility class.

Flex actually already has a utility class called ArrayUtil and a function which does what you want: ArrayUtil.getItemIndex.

var obj1:Object = new Object(); var obj2:Object = new Object(); var myArray : Array = [obj1, obj2]; ArrayUtil.getItemIndex(obj1, myArray); // returns 0 ArrayUtil.getItemIndex(obj2, myArray); // returns 1 ArrayUtil.getItemIndex(obj3, myArray); // returns -1

http://livedocs.adobe.com/flex/3/langref/mx/utils/ArrayUtil.html

RickDT