首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Javascript中创建一个自定义对象数组?

如何在Javascript中创建一个自定义对象数组?
EN

Stack Overflow用户
提问于 2012-06-30 21:40:42
回答 1查看 103关注 0票数 1

所以,我有一个友好的邻里对象构造函数,就像这样;

代码语言:javascript
复制
function Clip(a, b)
{
    this.track = a
    this.slot = b
    this.path = "live_set tracks " + a + " clip_slots " + b + " clip "
    clip = new LiveAPI(this.patcher, this.path)
    this.length = clip.get("length")
}

我想做的是

  1. 向数组中添加任意数目的
  2. 当数组的长度达到8时,将该数组添加到一个新的“超级”数组中,并启动一个新数组。

换句话说,超级数组应该允许我通过clip[0][0].length - clip[0][7].lengthclip[1][0].length - clip[1][7].length等方式访问对象的属性和方法。

EN

回答 1

Stack Overflow用户

发布于 2012-06-30 22:02:50

这就是你要找的吗?我简化了一些代码,但总体思路似乎是合适的。

http://jsfiddle.net/bryandowning/pH6bU/

代码语言:javascript
复制
var superArr = [];

function Clip(a) {
    this.length = a;
}

/*
* num: number of clip collections to add to container
* max: number of clips per collection
* container: array to add collections to
*/
function addClips( num, max, container ){

    while(num--){

        // arr: a collection of clips
        var arr = [];

        for( var i = 0; i < max; i++ ){

            arr.push(
                // just did a random number for the length
                new Clip( Math.floor( Math.random() * 10 ) )
            );

        }

        container.push( arr );

    }

}


addClips( 5, 8, superArr );

console.log( superArr );

console.log( superArr[0][0].length );


​
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11277890

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档