javascript - Visibility of "this" in Arrow Functions -


i have 2 cases

const test = {     foo: function (){         this.bar();     },     bar: function (){         console.log('bar');     } } test.foo(); 

in case, works correctly.

const test = {     foo: () => {         this.bar();     },     bar: () => {         console.log('bar');     } } test.foo(); 

in second case error:

uncaught typeerror: cannot read property 'bar' of undefined 

i know can wrote test.bar() in foo function, i'm interested why this not available in arrow functions scope in case.

normally, value of this in function depends on how function called.

arrow functions import value of this scope in function created.

in middle of object literal, value of this depend on around object literal, won't object itself.


Comments

Popular posts from this blog

sql - MySQL - Finding Empty relations -

javascript - Why jQuery Select box change event is now working? -

date - Java 8 - Trying to convert String to LocalDateTime -