How to access to a variable from another folder


Suppose we have a variable in login folder in “login-view model.js” and I want to access it from another folder in “another-view-model.js”.How can I do that in Javascript?


Create a singleton of the login view model, access it from every other page / view model you have.

login-view model.js

const instance;
exports.getViewModel = function() {
  if (!instance) {
    instance = createViewModelInstance();
  return instance;

Now whenever / wherever you call getViewModel() its going to return single shared instance.


I wrote exactly the same code you gave but my editor couldn’t automatically find neither ‘getViewModel’ nor ‘createViewModelInstance’ methods when I pressed Ctrl+Space.Is there anything I miss?


Sorry, when I say createViewModelInstance() I meant you shall call your function that will create the view model instance or directly write instance = new YourViewModelClassName().