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 8621255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:46:47+00:00 2026-06-12T06:46:47+00:00

Im having trouble setting variables within a requireJS Define module, it flatout doesnt take

  • 0

Im having trouble setting variables within a requireJS Define module, it flatout doesnt take effect. I have the following (cutdown) example to illustrate the issue:

Here is the defined module:

define([], function () {
var testerString = "BaseLine";

var ShowTesterString = function () {
    console.log(testerString);
}

var setTesterString = function (pTesterString) {
    testerString = pTesterString;
}

return {
    testerString: testerString,
    ShowTesterString: ShowTesterString,
    setTesterString: setTesterString
};  
});

I than set the “TesterModule” as a dependency of another and run the following lines of code:

TesterModule.ShowTesterString();
TesterModule.testerString = "Change 1";
TesterModule.ShowTesterString();
TesterModule.setTesterString("Change in Setter");
TesterModule.ShowTesterString();

What gets outputted to the console is:

BaseLine 
BaseLine 
Change in Setter 

I would have thought that it should be :

BaseLine 
Change 1
Change in Setter 

It seems that setting a variable in a module just by doing variable = blah doesnt take any effect, it seems to require to set variables within a method of the module. Can anyone explain to me why this is? Or what I am coding wrong?

Thanks in advance

  • 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-12T06:46:48+00:00Added an answer on June 12, 2026 at 6:46 am

    As far as I can tell, not being super-familiar with RequireJS, this is a pure-JavaScript problem. Change

    console.log(testerString);
    // to
    console.log(this.testerString);
    

    and change

    testerString = pTesterString;
    // to
    this.testerString = pTesterString;
    

    so that you’re getting/setting the value in the object returned by the anonymous function, rather than closing over the var testerString variable. What’s happening in your code currently may be clarified if you use variable names that differ from the returned object keys:

    define([], function () {
        var foo = "BaseLine";
    
        var ShowTesterString = function () {
            console.log(foo);
        }
    
        var setTesterString = function (pTesterString) {
            foo = pTesterString;
        }
    
        return {
            testerString: foo,
            ShowTesterString: ShowTesterString,
            setTesterString: setTesterString
        };  
    });
    

    Also, as a matter of style, it’s better to use a “normal” function declaration:

    function ShowTesterString () {
        ...
    }
    

    than to use var ShowTesterString = function () { ... }, as in your code.

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

Sidebar

Related Questions

I'm having trouble with setting variables for some opening and closing menus. I need
I am having some trouble setting the DJANGO_SETTINGS_MODULE for my Django project. I have
I'm having trouble with PHP session variables: I try setting the variables, and they
I'm having trouble assigning some variables as within a closure scope of a javascript
I am having trouble setting my ContentProperty to Text. The error I am given
I'm having trouble setting up my svn. I used apt-get install subversion to install
I'm having trouble setting the command-line arguments in Eclipse. I've searched around the Internet
I'm having trouble setting the path to a DLL that is not in the
I am having trouble setting the XHR responseType to json. It works fine if
I am having trouble setting up ejabberd to use PostgreSQL. My environment is OSX

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.