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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:34:24+00:00 2026-06-15T19:34:24+00:00

I have seen people use an alternate define syntax in require js than what

  • 0

I have seen people use an alternate define “syntax” in require js than what is described in the require js’ documentation, or in many tutorials.

The usual define “syntax”:

define(['module/first'], function (firstModule) {
   //Module code with a dependency on module/first goes here.
});

The alternate define “syntax”:

<script data-main="app/config" src="assets/js/libs/require.js"></script>

file: config.js:

require.config({
   paths: {
      jquery:      '../assets/js/libs/jquery'
   }
});
require(['app']);

file: app.js:

define(function(require) {
     var FirstModule = require('modules/first');
     //Module code with a dependency on module/first goes here.

What are the advantages, and disadvantages of using this alternate “syntax”?

  • 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-15T19:34:25+00:00Added an answer on June 15, 2026 at 7:34 pm

    I think your explanation is a bit misleading: in both cases, you will have a top-level require call with a data-main attribute specifying a file to kick-start the process of requiring different modules.

    So typically you will have this in your HTML:

    <script data-main="app/config" src="assets/js/libs/require.js"></script>
    

    Then, in both cases, you would have a file app/config which sets your configuration (although you could do this directly in the HTML) and more importantly calls require on your modules:

    require.config({
      paths: {
        jquery:      '../assets/js/libs/jquery'
      }
    });
    require(['app']);
    

    Now, it’s when we get to defining modules with dependencies that these styles differ. In the amd style you pass in the module names (paths) as an array, and a function which takes the same number of arguments:

    app.js

    define(['module/first', 'module/second', 'module/third'], function (firstModule, secondModule, thirdModule) {
      // use firstModule, secondModule, thirdModule here
    });
    

    In the simplified CommonJS syntax, you just pass require into define and then require whatever modules you need inline:

    app.js

    define(function(require) {
      var firstModule = require('modules/first');
      var secondModule = require('modules/second');
      var thirdModule = require('modules/third');
      // use firstModule, secondModule, thirdModule here
    
    }
    

    Getting back to your original question, the advantages of the CommonJS style over the amd style should be clear.

    For one thing, with the conventional syntax, if there are many modules being required, it is very easy to mistakenly assign modules to the wrong variable names. Consider this common case:

    define(['jquery', 'underscore', 'backbone', 'modules/first', 'modules/second', 'modules/third', 'i18n', 'someOtherModule'], function ($, _, Backbone, first, second, third, I18n, someOtherModule) {
      // ...
    });
    

    Right away, you can see that when we add a new module to this list, we have to be very careful that the corresponding new function argument appears in the right place, or else we can have jQuery assigned to Backbone, etc. In some cases this can create very subtle bugs that are hard to track down.

    Now consider the CommonJS syntax:

    define(function(require) {
      var $ = require('jquery');
      var _ = require('underscore');
      var Backbone = require('backbone');
      var firstModule = require('modules/first');
      var secondModule = require('modules/second');
      var thirdModule = require('modules/third');
      var I18n = require('i18n');
      var someOtherModule = require('someOtherModule');
      // ...
    }
    

    Note that:

    1. The pairing of module to variable name is very clear.
    2. The order of the require statements is not important, since the variable names are being paired separately rather than as a mapping between an array and a function.
    3. The modules do not need to be assigned first. They can be assigned anywhere, so long as it is before the module is actually used.

    Those are just a few reasons that come to mind, I’m sure there are others. Basically, if you just have one or two dependencies, either syntax will do fine. But if you have a complex network of module dependencies, the CommonJS syntax is probably preferable.

    Note that in the RequireJS docs, they mention this small caveat:

    Not all browsers give a usable Function.prototype.toString() results. As of October 2011, the PS 3 and older Opera Mobile browsers do not. Those browsers are more likely to need an optimized build of the modules for network/device limitations, so just do a build with an optimizer that knows how to convert these files to the normalized dependency array form, like the RequireJS optimizer.

    But this is not a major issue:

    Since the number of browsers that cannot support this toString() scanning is very small, it is safe to use this sugared forms for all your modules, particularly if you like to line up the dependency names with the variables that will hold their module values.

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

Sidebar

Related Questions

I have seen that people use <div id=name> rather than <div id=name> is that
People use Backbone.View in many ways, and I'm kind of confused. I have seen:
I have seen many people suggesting to use dispatch_once to do the singleton: +(MyClass
I have seen some instances where people will say you have to use JS
I have seen in many times in JavaScript code people add a return true
I have seen different people use different types of braces/brackets for this. I tried
I have seen people use GO statement between batches of SQL code, but AFAICS
I have seen people often use a separate table to store relations between tables.
Simple question... I have seen people tell me to use \r\n in various places
I have seen people use rowCount like this... if ($q -> rowCount() < 1)

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.