Javascript 跨回调函数共享数据
我有以下两个回调函数。我想知道有没有可能在clipname和has_clip函数之间共享names对象?这是在为ableton使用liveapi,但我确信这更像是一种通用javascriptJavascript 跨回调函数共享数据,javascript,function,callback,Javascript,Function,Callback,我有以下两个回调函数。我想知道有没有可能在clipname和has_clip函数之间共享names对象?这是在为ableton使用liveapi,但我确信这更像是一种通用javascript function loadclips() { names = new LiveAPI(this.patcher, 1, clipname, “live_set tracks 0 clip_slots 1 clip”); names.property = “name”; slot = new
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);
}
我认为最安全的方法是从loadClips返回一个对象(看起来也很合理)。确保对新变量使用
var
。可能会引入难以发现的bug
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);
我认为这更接近你所需要的 注意你的全球范围
names
是一个全局函数,但我怀疑您是否希望它是全局函数。我尝试过这个函数,它似乎是对的,但在ableton的上下文中它不起作用。我无法使“slot.property=“has_clip”;”显示在clipname函数中。在var slot=new LiveAPI
之后,尝试将slot对象记录到控制台,结果是“未定义”——查看该LiveAPI返回了什么call@Ke. 你修好了吗?