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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:23:19+00:00 2026-06-05T09:23:19+00:00

I realize I could answer myself with a simple test project (and I may,

  • 0

I realize I could answer myself with a simple test project (and I may, if no-one chimes in right away), but I couldn’t find the answer anywhere on SO or google, and it seems critical:

If I define an AMD module with require.js as such:

//a.js
define( ['stuff'], function (Stuff) {
     return { thing: new Stuff() };
}

And then I use it in two different other modules as such:

// b.js
define( ['a'], function(a) {
    // do something with a's stuff 
});

// c.js
define( ['a'], function(a) {
  //do something else with a's stuff
}

Does a‘s defining function get called (and therefore a new Stuff instantiated) each time I require it for another module or does it just get called once, and its output gets cached?

Obviously this is critical in some use-cases, but its not clear from the require.js documentation or other examples I’ve seen.

  • 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-05T09:23:22+00:00Added an answer on June 5, 2026 at 9:23 am

    I just tested this out myself, and it looks like the constructor is only run once.

    // stuff.js
    define(function() {
        return function() {
            console.log('making new!');
        };
    });
    
    // a.js
    define( ['stuff'], function (Stuff) {
         return { thing: new Stuff };
    });
    
    // b.js
    define( ['a'], function(a) {
    });
    
    // c.js
    define( ['a'], function(a) {
    });
    
    // app.js
    define(['b', 'c'], function() {
        console.log('app');
    });
    
    // index.html
    <html>
    <head>
        <script src='requirejs/require.js' data-main='app.js'></script>
    </head>
    </html>
    

    When I open index.html, the console shows:

    making new!                             stuff.js:3
    app                                       app.js:2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I realise there might be similar questions but I couldn't find one that was
I realize I'm going to get flamed for not simply writing a test myself...
I have tried looking for answer in the forum but couldnt find any which
I find myself writing a lot of reusable code because I couldn't find reusable
I realize that this question is impossible to answer absolutely, but I'm only after
This question may be easy and the answer obvious, but I can't seem to
I realize there are better compilers out there but I have preferred to stick
I realize the following function calls are all same, but I do not understand
I realize this question has been asked several times, but I wanted to frame
I'm trying to familiarize myself with the STL library, but I'm having trouble understanding

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.