Skip to content

Instantly share code, notes, and snippets.

@brandonpapworth
Created December 8, 2015 05:09
Show Gist options
  • Save brandonpapworth/84eaa4099fa402091160 to your computer and use it in GitHub Desktop.
Save brandonpapworth/84eaa4099fa402091160 to your computer and use it in GitHub Desktop.
Fuck this shit while knowing what "this" is.
'use strict';
function Shit() {
this.fo = 'shizzle';
}
Shit.prototype.fuck = function() {
console.log('Fuck', this, 'shit!');
};
var thisShit = new Shit();
thisShit.fuck(); // will work as expected.
// it is being called in the context of 'thisShit'
var fuckWhateverImCalledIn = thisShit.fuck;
fuckWhateverImCalledIn(); // will break, error-out, or do something unexpected.
// it is being called in a different/undefined context than 'thisShit'
var fuckThisShit = thisShit.fuck.bind(thisShit);
fuckThisShit(); // will have 'this' be equal to 'thisShit'
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
// for more information.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment