Skip to content

Instantly share code, notes, and snippets.

@ktajpuri
Created February 26, 2019 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ktajpuri/420868d56d220f1dd2f8d01ef0091e51 to your computer and use it in GitHub Desktop.
Save ktajpuri/420868d56d220f1dd2f8d01ef0091e51 to your computer and use it in GitHub Desktop.
how-this-differs-in-arrow-functions
this.myVar = "outer";
var obj = {
myVar: "inner",
myFunc1: function() {
console.log(this.myVar);
},
myFunc2: () => {
console.log(this.myVar);
},
myFunc3: function() {
setTimeout(function() {
console.log(this.myVar);
}, 1000);
},
myFunc4: function() {
setTimeout(() => {
console.log(this.myVar);
}, 1000);
}
};
obj.myFunc1(); //inner
obj.myFunc2(); //outer
obj.myFunc3(); //outer
obj.myFunc4(); //inner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment