Created
December 8, 2015 05:09
-
-
Save brandonpapworth/84eaa4099fa402091160 to your computer and use it in GitHub Desktop.
Fuck this shit while knowing what "this" is.
This file contains 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
'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