Posts

Showing posts with the label ArrayObject

More standard Stack, and more slim ArrayObject

I have just updated both Stack and ArrayObject constructors. The Stack improvement is About concat method, now truly standard, accepting arguments that are not instance of Stack or Array. var s = new Stack(1, 2, 3); alert(s.concat([4, 5], 6)); // 1,2,3,4,5,6 Since this method uses defined slice one, there is no more reason to redefine concat method in inherited prototypes. That is why ArrayObject does not need anymore concat method, for a total of 4 fast redefined methods, plus inherited concat. ArrayObject is now the fastest extended Array constructor that returns instances of the same constructor, ArrayObject. ArrayObject is, at the same time, the base to create every other kind of cool library, using native Array methods power. Enjoy!

Stack and ArrayObject - How to create an advanced subclassed Array constructor

Another problem left to solve is that after subclassing an Array one would like the return types of Array.prototype methods to be of the subclass type instead of its superclass type Array. This comment is, basically, a summary of the reason I created the ArrayObject constructor . Today, I have totally rewrote that constructor, using a Stack instance as prototype. The Stack constructor aim is to subclass the Array one in the most compatible, and light, way. For this reason, I have not changed native Array behaviours, those that return an Array, for example, instead of a Stack instance (concat, filter, map, slice). But the good thing of Stack, is that now we can create our subclassed Array constructor, without affecting the native Array object, and adding, modifying, or removing, whatever we want. For example, to solve the problem described on top of this post, I have simply modified inherited Stack methods, returning an instance of ArrayObject , every time we use, for example, a conca...

JavaScript ArrayObject

I think this constructor could be a good start point for a lot of projects. We know that IE has problems while it try to extend an array, disabling length modification. That's why I have created this wrapper that does not require weird strategies (e.g. iframe with a different enviroment) and works with a wide range of browsers. Honestly, I did not test performances against pure arrays, but I did everything to make code as fast as possible, using good practices and creating an in-scope shortcut for the Array.prototype. Have a look here if you are interested in this constructor, ignore them if you do not think this is a good idea :) P.S. Please note that this constructor does not care about missed prototypes, it only does its wrapping work and nothing else. To improve Array compatibility and methods, please remember my JSL Revision . Including them in your page, You'll not have problems with every ArrayObject method, eccept for reduce and reduceRight (I will create a good implem...