Well, you just wouldn’t call it on arrays that you were putting non-numbers into.
The only real reason is that your example extending array is really just creating a class with Array’s methods. You loose length and maybe some other goodness that makes Array iteration fast, for example. On Fri, Dec 6, 2013 at 11:29 AM, Regis Gaughan <[email protected]>wrote: > So there is a reason? ;) > > In this example, I wouldn't want Array to implement "increment" as a > method -- only Numbers should have increment available (just as Array > doesn't have "addClass" but Elements does). The example may be > trivial, but if it was a list of more complex instances it becomes > more evident. > > On Fri, Dec 6, 2013 at 2:22 PM, Aaron Newton <[email protected]> wrote: > > Why not just extend Array? > > > > > > On Fri, Dec 6, 2013 at 11:21 AM, Regis <[email protected]> wrote: > >> > >> I want to create a class, similar to Elements (just, not with elements) > >> which behaves like an array, but with additional methods on it that can > >> manipulate the objects within (which are all of the same type). I'm > aware > >> that the magical "length" behavior wouldn't work as expected. > >> > >> For example, is there any reason why something like this would not work > as > >> expected (as it seems to just fine): > >> > >> var Numbers = new Class({ Extends: Array, increment: function(){ var > self > >> = this; self.forEach(function(n, i){ self[i]++; }); return self; } }); > >> var n = new Numbers(); n.include(2).combine([3,4,5]).unshift(1); // > >> [1,2,3,4,5] > >> n.increment(); // [2,3,4,5,6] > >> > >> > >> -- > >> > >> --- > >> You received this message because you are subscribed to the Google > Groups > >> "MooTools Users" group. > >> To unsubscribe from this group and stop receiving emails from it, send > an > >> email to [email protected]. > >> For more options, visit https://groups.google.com/groups/opt_out. > > > > > > -- > > > > --- > > You received this message because you are subscribed to a topic in the > > Google Groups "MooTools Users" group. > > To unsubscribe from this topic, visit > > https://groups.google.com/d/topic/mootools-users/KBiU3Rq8SE8/unsubscribe > . > > To unsubscribe from this group and all its topics, send an email to > > [email protected]. > > For more options, visit https://groups.google.com/groups/opt_out. > > -- > > --- > You received this message because you are subscribed to the Google Groups > "MooTools Users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/groups/opt_out. > -- --- You received this message because you are subscribed to the Google Groups "MooTools Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/groups/opt_out.
