Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8652335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:17:07+00:00 2026-06-12T14:17:07+00:00

Is there a way to have a static variable in a dojo (1.8) module

  • 0

Is there a way to have a static variable in a dojo (1.8) module so that I can retain state?

Example, say I set some value in some/module

require([
     'some/module'
], function (module) {
    module.setSomeValue(3); 
});

.. and then want to retrieve it later

define([
    'some/module'
], function(module) {
    return {
        start: function() {
            var x = module.getSomeValue(); 
        }
    };
});

A solution that works but seems like a hack,

acompany = window.acompany || {};
acompany.project = acompany.project || {
};

require([       
], function() {
    var debug = false;

    acompany.project.module = {
        /* static variables and functions here */ 
    };
});

define([

], function () {
    return acompany.project.module;
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:17:09+00:00Added an answer on June 12, 2026 at 2:17 pm

    Actually there is always only single instance of AMD module, because (source):

    define has two additional important characteristics that may not be immediately obvious:

    • Module creation is lazy and asynchronous, and does not occur immediately when define is called. This means that factory is not executed, and any dependencies of the module will not be resolved, until some running code actually requires the module.
    • Once a module value has been entered into the module namespace, it is not recomputed each time it is demanded. On a practical level, this means that factory is only invoked once, and the returned value is cached and shared among all code that uses a given module. (Note: the dojo loader includes the nonstandard function require.undef, which undefines a module value.)

    In addition you do not have to provide just factory function, you can provide an object literal as well:

    define("some/module", {
        someValue: "some",
        otherValue: "other"
    });
    

    Then somewhere else in your code:

    require(["some/module"], function(module) {
        console.log("module.someValue", module.someValue); // some
        module.someValue = "some changed";
    });
    
    require(["some/module"], function(module) {
        console.log("module.someValue", module.someValue); // some changed
    });
    

    More robust solution includes an instance of dojo/Stateful, so you can watch for changes and define custom setters and getters:

    define("some/stateful-module", ["dojo/Stateful"], function(Stateful){
        var stateful = new Stateful({
            someValue: "some",
            otherValue: "other"       
        });
        return stateful;
    });
    

    Then somewhere else in your code:

    require(["some/stateful-module"], function(module) {
        console.log("stateful-module.someValue:", module.get("someValue"));
        module.watch(function(name, oldValue, newValue) {
            console.log("stateful-module: property"
                , name
                , "changed from"
                , "'" + oldValue + "'"
                ,  "to"
                , "'" + newValue + "'"
            );        
        });
    });
    
    require(["some/stateful-module"], function(module) {
        module.set("someValue", "some changed");
    });​
    

    See how it works at jsFiddle: http://jsfiddle.net/phusick/fHvZf/. It’s in a single file there, but it will work the same way across the whole application unless you require.undef(mid) the module.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to have a mutable static variable in F# class that
If you have a template class with a static variable, is there any way
I was wondering, is there a way to have a sort of variable that
Is there a way to force classes in Java to have public static final
I need a way to have a single static variable for all kinds of
Is there a way to have an global settings variable for an android application,
Is there a way to share one static variable between several different generic classes
Is there a way to have setup/teardown code automagically run before/after each test? Something
Is there a way to have a template specialization based on a range of
Is there a way to have ASP classic cdonts email, have an file attached

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.