2012-06-30 55 views
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,该阵列添加到新的“超级“阵列并启动一个新阵列。

换句话说,在superarray应该让我通过访问对象的属性和方法,例如,clip[0][0].length - clip[0][7].lengthclip[1][0].length - clip[1][7].length

+0

和谐代理。 –

+0

@RobW现在使用谷歌搜索... – Pointy

+0

array,superarray ...为什么不用对象来处理呢? – elclanrs

回答

0

这是你在找什么?我简化了一些代码,但总的想法似乎是合适的。

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

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

所以基本上你是说,从函数外部创建一个全局数组,然后在参数中引用它,然后执行此操作? – jamesson

+0

'container'只是'addClips'函数的一个命名参数。在addClips函数的范围之外,实际上没有任何叫'container'的东西。在我的例子中,我创建了一个名为'superArr'的全局数组。它不一定是全球性的,它只需要从你调用addclip的地方访问即可。 'superArr'被传递给'addClips',这使得它成为'addClips'范围内的'容器'。合理? –

相关问题