Last active
September 26, 2019 07:18
-
-
Save AkashRajvanshi/1f141d4ec786bb868610699ab0bd89a0 to your computer and use it in GitHub Desktop.
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
// Example with Normal Function | |
// Value of "this" in this method is overridden to 3 | |
function foo() { | |
return function (value) { | |
console.log(this.value) // 3 | |
} | |
} | |
var obj1 = { | |
value: 2 | |
} | |
var obj2 = { | |
value: 3 | |
} | |
var bar = foo.call(obj1) | |
bar.call(obj2) | |
// But if we use arrow function then value of "this" will not overide | |
// Example with arrow function | |
function foo() { | |
return (value) => { | |
console.log(this.value) //2 | |
} | |
} | |
var obj1 = { | |
value: 2 | |
} | |
var obj2 = { | |
value: 3 | |
} | |
var bar = foo.call(obj1) | |
bar.call(obj2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment