简体   繁体   English

如何在javascript中使用自定义“类”(函数)作为数组数据类型?

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

I know in javascript if I want define an array,I use: 我知道在javascript中如果我想定义一个数组,我使用:

var arr=new array();

However,this array could only use for store date,int,string,just the javascript already defined 但是,这个数组只能用于存储日期,int,字符串,只是已定义的javascript

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"? 如何初始化数据类型为“LayerInfo”的数组,可以存储“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? 如果可以这样做,我怎么能让layerinfo推入数组,或将其拉出来?

However,this array could only use for store date,int,string,just the javascript already defined 但是,这个数组只能用于存储日期,int,字符串,只是已定义的javascript

Fortunately not, arr can store any object you like, including instances of your LayerInfo : 幸运的是, arr可以存储您喜欢的任何对象,包括LayerInfo实例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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