Skip to content

Instantly share code, notes, and snippets.

@stefbowerman
Last active October 6, 2015 20:21
Show Gist options
  • Select an option

  • Save stefbowerman/12618f51ec8556143673 to your computer and use it in GitHub Desktop.

Select an option

Save stefbowerman/12618f51ec8556143673 to your computer and use it in GitHub Desktop.
var Thing = function(){
var _this = this;
this.checkContext = function(){
console.group()
console.log('Checking context.....');
console.log('this', this); // Outputs - Thing
$(window).on('check-context', function(){
console.log('inside window listener callback without context binding');
console.group()
console.log('this', this); // Outputs - Window
console.log('$(this)', $(this)); // Outputs - $(window);
console.log('_this', _this); // Outputs - Thing
console.log('Done checking context');
console.groupEnd();
console.groupEnd();
$(window).off('check-context');
});
$(window).trigger('check-context');
};
this.checkBoundContext = function(){
console.group()
console.log('Checking bound context.....');
console.log('this', this); // Outputs - Thing
$(window).on('check-bound-context', function(){
console.log('inside window listener callback with context binding');
console.group()
console.log('this', this); // Outputs - Thing
console.log('$(this)', $(this)); // Outputs - $(Thing);
console.log('_this', _this); // Outputs - Thing
console.log('Done checking bound context');
console.groupEnd();
console.groupEnd();
$(window).off('check-bound-context');
}.bind(this));
$(window).trigger('check-bound-context');
};
};
var t = new Thing();
t.checkContext();
t.checkBoundContext();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment