Search code examples
javascriptrequirejsjs-amd

using define() and require() in requireJS


I understand that define() is used to crete a module

require() is used to retrieve the data.

I have script1.js which contains

define(['dependency1'], function(dep1){
     var operation = {
         addition: function(a, b){
            return a + b;
         }
     };        
     return operation;
});

I use the code in script2.js

require(['script1'], function(x){
    console.log(x.addition(11, 8));
})

Is there a better way to use require and define to achieve the same result?


Solution

  • No "better" way I can think of--that looks right to me. (I'm more used to CommonJS/Browserify, but this still should work.)