简体   繁体   中英

sharing data across callback functions

I have the following two callback functions. Im wondering is it possible to share the names object between clipname and has_clip functions? This is using the liveapi for ableton, but im sure its just more of a general javascript thing.

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);
}

I think the safest thing would be to return an object from loadClips (seems sensible too). Make sure to use var on new variables. Global scope pollution can introduce hard-to-find bugs.

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
  }; 

}

Then pass that into any functions that might need it.

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

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

Now you can call loadClips:

var namesAndClips = loadClips(); 

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

I think that's closer to what you need anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM