[Javascript] custom extension of Array object

james james at southspace.org
Thu Apr 14 14:24:47 CDT 2005


At 19:22 14/04/2005 +0200, you wrote:
>On 4/14/05, james <james at southspace.org> wrote:
> > ok.. what I am wanting to do it this, I want to create a custom object
> > which inherits all the methods of the Array object, but I don't want to
> > alter the normal Array objects.
>
>Like this?
>
>     function CustomArray(){
>     }
>     CustomArray.prototype=new Array;

aha! when I try I did this

CustomArray.prototype = new Array()

and it didn't seem to work, but now I am not sure!

>     var
>         ca=new CustomArray;
>     ca.push('test');
>     ca.push('test');
>     ca.push('test');
>     alert(ca); // => test,test,test

great!

>Maybe you're addressing this in the wrong way, though. Is there a
>compelling reason for you to use a custom object and inheritance
>rather than using an array to which you have added the custom
>properties? Something like this, if you want to make it a function:
>
>     function CustomiseArray(arr){
>         if(typeof arr=='undefined'||arr.constructor!=Array)
>             arr=[];
>         arr.customMethod=function(){};
>         arr.customProperty='bite me!';
>         return arr;
>     }
>
>     var
>         ca=CustomiseArray();

no I think that is fine, I'll try it.

(it's going to take me a little while to work through this!)

Thanks

James 




More information about the Javascript mailing list