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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:38:33+00:00 2026-06-18T11:38:33+00:00

With my limited understanding of RequireJS and Node.js (plus JavaScript in general), I usually

  • 0

With my limited understanding of RequireJS and Node.js (plus JavaScript in general), I usually take a look at the source of some well-known JavaScript libraries. Every time I see something like this:

( // Wrapping
    function (root, factory) {
        if (typeof exports === 'object') { // Node.js

            var underscore = require('underscore');
            var backbone = require('backbone');

            module.exports = factory(underscore, backbone);

        } else if (typeof define === 'function' && define.amd) { // Require.JS

            define(['underscore', 'backbone'], factory);

        } 
    }(this, function (_, Backbone) { // Factory function, the implementation
        "option strict";

        function Foo() {}

        return Foo; // Export the constructor
    })
); // Wrapping

What I can understand (hopefully):

  • The anonymous function that wraps the code is automatically executed when the script is uncluded in a <script> tag
  • This code works with both RequireJS and Node.js (if checks in the very beginning); the result of factory function is either assigned to module.exports (Node.js) or used as argument of define function (RequireJS).

Q1: how this code works without RequireJS and Node.js? if and else if checks would fail, factory function is never executed and the scripts returns nothig.

Q2: what’s the purpose of passing this as root argument? It’s never used

  • 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-18T11:38:34+00:00Added an answer on June 18, 2026 at 11:38 am

    Actually I think the code snipped in your question will not work with browser globals. The pattern used in this snipped is called UMD – Universal Module Definition. In fact there are many variations of this pattern, you can browse more examples on https://github.com/umdjs/umd

    As for the questions:

    Q1
    This snippet will not work in browsers without RequireJS or any other AMD loader, for obvious reasons – there only two checks – for the NodeJS and define function, so without using AMD library the factory function won’t be called.

    To make the factory function called just add another condition for browser globals

    if (typeof exports === 'object') { // Node.js
        var underscore = require('underscore');
        var backbone = require('backbone');
        module.exports = factory(underscore, backbone);
    
    } else if (typeof define === 'function' && define.amd) { // Require.JS
         define(['underscore', 'backbone'], factory);
    } else {
        // Browser globals
        factory(root._, root.Backbone);
    }
    

    Note that we used root object passed to the wrapper function and as nekman pointed out it will be set to window in browser environment, so we just pass global objects defined on that window to the factory, these objects usually defined by other script tags on the page. Hope this answers your second question.

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

Sidebar

Related Questions

Why isn't every type of object implicitly serializable? In my limited understanding, are objects
In my limited experience, I've been on several projects that have had some sort
I've been using jQuery for a couple of years now with very limited understanding
My limited understanding of ThreadLocal is that it has resource leak issues . I
My limited understanding of the Fourier transform is that you should be able to
I have a limited understanding of Javascriptan I am trying to add a class
I am new to android and have very limited understanding of the code. I
I've been wrestling with my limited knowledge of real programming and need some help...
My limited understanding of C++ means that I have no idea how to correctly
From my limited understanding of nginx I know that nginx seperates itself from Apache

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.