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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:36:10+00:00 2026-06-07T18:36:10+00:00

Could someone explain the fundamental difference between: define([‘backbone’], function(Backbone) { MyModel = Backbone.Model.extend({ });

  • 0

Could someone explain the fundamental difference between:

define(['backbone'], function(Backbone) {
   MyModel = Backbone.Model.extend({
   });
});

define(['backbone', 'models/mymodel'], function(Backbone){
    var app = Backbone.View.extend({
        initialize: function() {
           var model = new MyModel();
        }
    });
});

and:

define(['backbone'], function(Backbone) {
   var MyModel = Backbone.Model.extend({
   });
   return MyModel;
});

define(['backbone', 'models/mymodel'], function(Backbone, MyModel){
    var app = Backbone.View.extend({
        initialize: function() {
           var model = new MyModel();
        }
    });
});

In the former, the first module simply defines MyModel. In the latter, it’s created as a variable and returned, and the second module needs to have it put in the parameters when imported.

RequireJS examples I see around seem to vary between the two, but I don’t really understand the difference – does one return an instance and the other a constructor?

In my application I didn’t even notice that I was actually using both ways in different places, and I think it was causing problems. I was using a lot of

self = this
self.model.doSomething

inside my views and models, and as my app got bigger, I started getting errors because there were conflicts with definitions of self.

  • 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-07T18:36:12+00:00Added an answer on June 7, 2026 at 6:36 pm

    Short Version: 1st version == wrong.

    Medium Version: The first one bypasses Require entirely by using global variables, while the second one actually uses Require.

    Long version:

    The way Backbone modules work is that you run “define”, pass it a function (and usually an array of dependencies also), and whatever gets returned from that function is defined as that module. So if I do:

    // Inside foo.js
    define([], function() {
       return 1;
    });
    

    I’ve defined the “foo” module to be 1, so if elsewhere I do:

    define(['foo'], function(foo) {
        alert(foo); // alerts 1
    });
    

    Your first version doesn’t return anything, so it’s not actually creating a Require module at all.

    How does it work then? Well, in that version you do:

    MyModel = Backbone.Model.extend({
    

    NOT:

    var MyModel = Backbone.Model.extend({
    

    So that’s really the same as doing:

    window.MyModel = Backbone.Model.extend({
    

    Then when the second part of the code runs, it access window.MyModel, and works … but it’s completely bypassing Require.js in the process.

    I think the most important thing to takeaway is: ALWAYS DECLARE (ie. var) YOUR JAVASCRIPT VARIABLES. I don’t agree with everything Crockford says, but he’s dead right on this one. You will get lots of bugs (with Require and without) if you don’t make this a habit.

    Beyond that, the next most important thing is probably: ALWAYS RETURN SOMETHING FROM THE FUNCTION YOU PASS TO define. There are certain special cases where you don’t want to return anything, but unless you are deliberately trying to solve one of those cases you should always return something to define the module.

    Finally, if you’re using Require, every variable in your code should either:

    • Come from the define function (ie. it should be an argument variable from the function that you pass to define), or
    • It should be declared (ie. var-ed ) inside that file

    If you use JSLint or 'use strict'; (as Valentin Nemcev suggested), or if you use an editor like Eclipse, your tools can help you ensure this (and in fact make it easy to ensure).

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

Sidebar

Related Questions

Could someone explain to the statistically naive what the difference between Multiple R-squared and
Could someone explain the difference between polynomial-time, non-polynomial-time, and exponential-time algorithms? For example, if
Could someone explain the difference between Software Design and Software Architecture? More specifically; if
Could someone explain the difference between the Copy Bundle Resources phase of Xcode and
Could someone explain to me why adding a virtual function to the end of
Could someone explain this to me? var diagramImage = new Kinetic.Shape(function () { var
Could someone explain differences and relations between Python/PHP, cakePHP and wordpress? Maybe draw some
Could someone explain what's the key difference among those concepts (MDD vs MDA vs
Could someone explain me what differences there are between strtok() and strsep() ? What
Could someone explain the differences between the two? Which would be better to learn

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.