One thing about putting these methods into a class -- it would be nice if they were a mixin so only Arrays that actually needed the methods got them. (Mixing into the Array prototype would add them to all Arrays if a user decided that would be worthwhile.)
I occasionally make utility classes (ArrayUtil) and exporter classes (ArrayUtilExporter) -- the utility class has methods like: ArrayUtil.contains( toCheck:Array, item:Object ) :Boolean and the exporter class is a mixin that adds the ArrayUtil methods to a given array. -mark > > From: Chris Hill <[EMAIL PROTECTED]> > Date: 2006/07/26 Wed PM 01:20:15 CDT > To: Flashcoders mailing list <flashcoders@chattyfig.figleaf.com> > Subject: Re: [Flashcoders] Array Empowerment > > Found a couple problems with sum(): > First off, instanceof works with *objects* not primitives, and so you > must do: > > var a:Array = new Array(new Number(2),new Number(3),new Number(54),new > Number(6),new Number(7),new Number(8),new Number(9)); > > instead of say: > > var b:Array = new Array(2,3,54,6,7,8,9); > > So you *have* to check with typeof for primitives, then instanceof for > objects. Horrible, I know. > > Additionally, it would help to have a return statement :D. > > Array.prototype.sum = function() { > var x = 0; > var a = this.length; > while (--a -(-1)) { > var num = this[a]; > if (typeof(num) == "number" || num instanceof Number) { > x += num; > } else { > return undefined; > } > } > return x; > }; > > //Here are the test cases i wrote to verify functionality > > var a:Array = new Array(new Number(2),new Number(3),new Number(54),new > Number(6),new Number(7),new Number(8),new Number(9)); > var b:Array = new Array(2,3,54,6,7,8,9); > var c:Array = new Array(2,new Number(3),new Number(54), 6,7,8,9); > var d:Array = new Array("2",3,4,new Number(54),6,7,8,9); //should return > undefined > trace(a.sum()); > trace(b.sum()); > trace(c.sum()); > trace(d.sum()); > > > Lastly, there were a couple places where you did not use 'var' and the > compiler caught it when I encapsulated your AS 1 into a class. I'll post > if you like. > > Peace > C > _______________________________________________ > Flashcoders@chattyfig.figleaf.com > To change your subscription options or search the archive: > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > > Brought to you by Fig Leaf Software > Premier Authorized Adobe Consulting and Training > http://www.figleaf.com > http://training.figleaf.com > -- John Mark Hawley The Nilbog Group 773.968.4980 (cell) _______________________________________________ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com