Last active
October 6, 2015 20:21
-
-
Save stefbowerman/12618f51ec8556143673 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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