I'm trying to access a variable from a namespace on the same page at a "global" level but thus far have been unable to do so. Here's the code:
window.addEventListener('resize',
() => {
alert('entityId in addEventListener alert: ' + Best.Namespace.Ever.entityId);
// prints "entityId in addEventListener alert: undefined"
});
var Best = Best || {};
Best.Namespace = Best.Namespace || {};
Best.Namespace.Ever= Best.Namespace.Ever || (function () {
var entityId;
function doSomethingAmazing(something) {
entityId = alterEntityIdBasedUponSomething(something);
alert('entityId in doSomethingAmazing: ' + entityId);
// prints "entityId in doSomethingAmazingalert: 14"
// a lot of other stuff
}
return {
doSomethingAmazing: doSomethingAmazing,
entityId: entityId
// a lot of other stuff
};
}());
The issue here is that I'm unable to access the entityId from within the alert in the addEventListener method. At that point it is undefined. However, I can access it in the alert just after it is set just fine. (See the comments in the code above for what prints.)
I feel like this should be possible and I just don't know the correct way to access entityId. If anyone has any ideas I would very much appreciate it.
Thanks!!
entityId: entityIdit will be that value, it will not update, you should be using a getter method.