You are modifying it when you call getID.
The difference is that you're using a mechanism which enforces a policy of access to that piece of global state.
Which should tell us that this problem isn't about global variables -- it's about policy enforcement.
If you have es6 modules available, I'd recommend using those to provide this kind of policy mechanism.
// userCounter.js
let id = 0; export const getNextUserId = () => id++;
// foo.js
import { getNextUserId } from './userCounter.js';
// bar.js
Which also solves the problem of ensuring that the same counter is used properly in each context that requires that counter.
Yeah you right, I just use a funny title. Yeah any encapsulation that protects the access to the variable is valid. Thanks for sharing 😊
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
You are modifying it when you call getID.
The difference is that you're using a mechanism which enforces a policy of access to that piece of global state.
Which should tell us that this problem isn't about global variables -- it's about policy enforcement.
If you have es6 modules available, I'd recommend using those to provide this kind of policy mechanism.
// userCounter.js
// foo.js
// bar.js
Which also solves the problem of ensuring that the same counter is used properly in each context that requires that counter.
Yeah you right, I just use a funny title. Yeah any encapsulation that protects the access to the variable is valid. Thanks for sharing 😊