myRegFunc
refers to this
with local scope limited to users
object. However, myArrowFunc
instead refers to this
with global scope. Is there a way to reference the same this
information in the arrow function as is available from the regular function?
So the arrow func cannot bind to the local this? However, is it possible to still get the local this information through the global this? For example: global this.something.something ... = the local this.
const users = {
name: 'John',
myRegFunc: function() {
console.log('myRegFunc : ', this);
},
myArrowFunc: () => console.log('myArrowFunc this: ', this),
};
users.myRegFunc();
users.myArrowFunc();
Output:
myRegFunc : {name: 'John', myRegFunc: ƒ, myArrowFunc: ƒ}
arrowFunc.js:34 myArrowFunc this: Window {window: Window, self: Window, document: document, name: '', location: Location, …}