Created
November 28, 2016 05:00
-
-
Save domfarolino/405774b3a0fb00070aed257cc56804c9 to your computer and use it in GitHub Desktop.
Simple es6 lexical this vs that=this/.bind(...) example
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
function CoolObject() { | |
var that = this; | |
// Allows us to use lexical this as opposed to .bind or that = this trick | |
this.coolPromise = new Promise((resolve, reject) => { | |
console.log(this === that); | |
setTimeout(() => { | |
resolve(10); | |
}, 2000); | |
}); | |
// this.coolPromise = new Promise(function(resolve, reject) { | |
// console.log(this === that); | |
// setTimeout(() => { | |
// resolve(10); | |
// }, 2000); | |
// }.bind(this)); | |
} | |
const coolObj = new CoolObject(); | |
coolObj.coolPromise.then(v => { | |
console.log(v); | |
console.log(this == window); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment