Skip to content

Instantly share code, notes, and snippets.

@domfarolino
Created November 28, 2016 05:00
Show Gist options
  • Save domfarolino/405774b3a0fb00070aed257cc56804c9 to your computer and use it in GitHub Desktop.
Save domfarolino/405774b3a0fb00070aed257cc56804c9 to your computer and use it in GitHub Desktop.
Simple es6 lexical this vs that=this/.bind(...) example
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