Skip to content

Latest commit

 

History

History
35 lines (27 loc) · 657 Bytes

context.md

File metadata and controls

35 lines (27 loc) · 657 Bytes

Ways to pass/keep context:

  1. Anonymous callback, e.g.

    var context = {
      callback: function(){
        // ...
      }
    };
    
    asyncThing(function(){
      // if it's a property,
      context.callback();
      // if not,
      callback.apply(context);
    });
  2. Pass the context and the function as parameters, e.g.

    asyncThing(callback, context);
  3. Using bind(), e.g.

    // homegrown
    var boundCallback = bind(callback, context);
    // or built-in (to newer browsers)
    var boundCallback = callback.bind(context);
    
    asyncThing(boundCallback);