2013-09-25 76 views
0

我有以下兩個回調函數。我想知道是否有可能在clipname和has_clip函數之間共享名稱對象?這是爲使用liveton而開發的,但是我確定它只是一個普通的javascript事物。跨回調函數共享數據

function loadclips() { 

    names = new LiveAPI(this.patcher, 1, clipname, 「live_set tracks 0 clip_slots 1 clip」); 
    names.property = 「name」; 

    slot = new LiveAPI(this.patcher, 1, has_clip, 「live_set tracks 0 clip_slots 1」); 
    slot.property = 「has_clip」; 

} 

function clipname(args) { 
    post(args); 
} 

function has_clip(args) { 
    post(args); 
} 
+0

關注您的全球範圍; '名稱'是一個全球性的,但我懷疑你想要它。 – jcollum

回答

1

我認爲最安全的事情是從loadClips返回一個對象(似乎也很明智)。確保在新變量上使用varGlobal scope pollution可能會引入難以發現的錯誤。

function loadclips() { 

    var names = new LiveAPI(this.patcher, 1, clipname, 「live_set tracks 0 clip_slots 1 clip」); 
    names.property = 「name」; 

    var slot = new LiveAPI(this.patcher, 1, has_clip, 「live_set tracks 0 clip_slots 1」); 
    slot.property = 「has_clip」; 

    return { 
    names: names, 
    slot: slot 
    }; 

} 

然後將其傳遞給任何可能需要它的函數。

function clipname(args, namesAndSlots) { 
    // namesAndSlots is available here 
    post(args); 
} 

function has_clip(args, namesAndSlots) { 
    // namesAndSlots is available here 
    post(args); 
} 

現在,您可以撥打loadClips:

var namesAndClips = loadClips(); 

var clip = clipName('a', namesAndClips); 

我覺得這是更接近你所需要的反正。

+0

我試過了,它看起來沒錯,但是在函數的背景下它不起作用。我無法得到「slot.property =」has_clip「;」顯示在剪輯名稱功能中。它只是作爲「未定義」 –

+0

嘗試在'var slot = new LiveAPI'之後將插槽對象記錄到控制檯 - 請參閱LiveAPI呼叫返回的內容 – jcollum

+0

@Ke。你修好了嗎? – jcollum