There’s actually two reasons for that:
-
this usually (will explain later) scopes to the parent object of the module (function inside an object) in which it’s written. For example, if you had written the function as a module of inventory like so:
const inventory = {
sunglasses: 1,
pants: 1088,
bags: 1344,
myExecutor(resolve, reject) {
if(this.sunglasses > 0) {
resolve('Sunglasses order processed.');
} else {
reject('That item is sold out.');
};
}
};
the this.sunglasses, would reference as intended by you, because this refers to inventory, the parent of the module (function) myExecutor.
Important: notice that I did NOT use an arrow function in declaring my module, as that would not work. I COULD have used a normal function declaration though function myExecutor () {};
, and that would have worked just aswell.
Which brings us to point two:
-
this does not work the same way inside arrow functions.
In an arrow function the keyword this isn’t bound to it’s functions parent (object in which function is inserted), and I recomend reading documentation on it.
Disclaimer: don’t trust my every word as absolute true, as I’m also still learning.