简体   繁体   中英

How can I use custom “class”(function) as array datatype in javascript?

I know in javascript if I want define an array,I use:

var arr=new array();

However,this array could only use for store date,int,string,just the javascript already defined

Suppose I have already my custom datatype:

        function LayerInfo(uuid, top, left, index, pindex) {
            this.uuid = uuid;
            this.top = top;
            this.left = left;
            this.index = index;
            this.pindex = pindex;
        }

How can I initialize an array which the data type is the "LayerInfo",could store the "LayerInfo"?

like

var l=new LayerInfo()?

if this could be done ,how can I let the layerinfo push into the array,or pull it out?

However,this array could only use for store date,int,string,just the javascript already defined

Fortunately not, arr can store any object you like, including instances of your LayerInfo :

var arr = new Array(); //note caps or var arr = [];
arr[i] = new LayerInfo();
arr[i] = someLaterInfoInstance;
arr.push(new LayerInfo()); ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM