-1

I have read this question & I want to understand this concept more clearly so I have created my two files

lorem.js:

var lorem = {};

lorem.fun1 = function(){
    console.log('aaa');
};

ipsum.js:

var ipsum = {};
ipsum.color = 'a';

ipsum.fun1 = function(){
    lorem.fun1();
};

Ipsum depends on lorem. In the shim configuration I have done something like this:

// Filename: main.js

// Require.js allows us to configure shortcut alias
// There usage will become more apparent further along in the tutorial.
require.config({
    baseUrl: 'js',
    paths: {
        /*jquery: [
            // 'https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min' ,
            'libs/jquery/jquery'
        ],
        backbone:[
            'libs/backbone/backbone'
        ],
        underscore:[
            'libs/underscore/underscore'
        ],*/
        ipsum: 'nonamd/ipsum',
        lorem: 'nonamd/lorem'
    },
    shim : {
        /*jquery : {
            exports : 'jQuery'
        },
        underscore : {
            exports : '_'
        },
        backbone : {
            deps : ['jquery', 'underscore'],
            exports : 'Backbone'
        },*/
        ipsum : {
            deps : ['lorem'],
            exports : 'Ipsum'
        }
    }

});

require(['ipsum'],function (Ipsum) {
    console.log(Ipsum);
});

However, console.log(Ipsum) prints undefined in console. What am I doing wrong?

Community
  • 1
  • 1
anasanjaria
  • 1,308
  • 1
  • 15
  • 19

1 Answers1

2

Your mistake is in the shim configuration.

  ipsum : {
            deps : ['lorem'],
            exports : 'Ipsum'
        }

should be

  ipsum : {
            deps : ['lorem'],
            exports : 'ipsum'
        }

Note the case change from 'Ipsum' to 'ipsum'.

ipsum should be a global variable in ipsum.js.

In Javascript, variable names are case sensitive, so ipsum and Ipsum are different. Because you hadn't defined Ipsum, only ipsum, printing it printed undefined.

Nic
  • 6,211
  • 10
  • 46
  • 69
Vladu Ionut
  • 8,075
  • 1
  • 19
  • 30