1

I'd like to create a variable that will be accessible from any module inside my RequireJS project.

For example, on init I'd like to set:

this.myVar = 123;

and be able to access it in any other module inside (but not outside) my RequireJS project such as:

console.log(this.myVar);

Is this possible?

P.S - I'm using 'this' just for an example. Any other option is viable.

P.S 2 - The RequireJS is actually a widget that can be instantiated several times such as :

new Widget('w1');
new Widget('w2');

and the global parameter should be inside each instance and not shareable between them.

EDIT (Example for such possible code, which doesn't work):

define('module1', [], function() {
    var myVar = 123;
});

define('module2', ['module1'], function(m1) {
    console.log(myVar); // Should print '123'
});
6
  • var myModuleScopedVariable = 123 ? Commented Sep 8, 2014 at 9:31
  • I Wish :) It doesn't work in other modules - just the current one. Commented Sep 8, 2014 at 9:33
  • I've added an example for the code Commented Sep 8, 2014 at 9:35
  • Create a settings module and whoever needs to access the settings imports it - have it export an object with whatever variables you want. Commented Sep 8, 2014 at 9:39
  • Perfect Thanks! Works perfectly! If you could write this as an answer - I could accept it :) Commented Sep 8, 2014 at 9:44

1 Answer 1

2

Create a Settings module and whoever needs to access the settings imports it - have it export an object with whatever variables you want.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.