2012-06-30 91 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'範圍內的'容器'。合理? –

相關問題