javascript - Visibility of "this" in Arrow Functions -
this question has answer here:
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
Post a Comment