1.

When Should I Use Require () And When To Use Define ()?

Answer»

The define () method looks like:-

  • The define () method is used for facilitating module definition
  • The define () method accepts two optional parameters module ID and array of required modules [dependencies]
  • The define () method MUST return the implementation for your module i.e.

define(

module_id /*optional*/,

[dependencies] /*optional*/,

definition function /*function for instantiating the module or object*/

);

And

define (['moduleA', 'moduleB'], function (moduleA, moduleB) {

//define the module value by returning a value

return function () {};

});

The require () method looks like:-

  • The require () method is used for handling dependency loading
  • The require () function doesn't have to return the implementation of a new module.

require(['jquery'], function ($) {

//jQuery was loaded and can be used now.

});

Example 1:-

define( 'MyApp', ["marionette"], function (Marionette) {

// SET up the app instance

var app = new Marionette.Application();

app.on("initialize:after", function(){

console.log("initialize started.");

});

// export the app from this module

return app;

});

Example 2:-

// FETCH and EXECUTE Marionette App

require( ["MyApp"], function (app) {

// Execute App

app.start();

});

Example 3:-

define({

"root": {

"india": "india",

"australia": "australia",

"england": "england"

}

});

The define () method looks like:-

define(

module_id /*optional*/,

[dependencies] /*optional*/,

definition function /*function for instantiating the module or object*/

);

And

define (['moduleA', 'moduleB'], function (moduleA, moduleB) {

//define the module value by returning a value

return function () {};

});

The require () method looks like:-

require(['jquery'], function ($) {

//jQuery was loaded and can be used now.

});

Example 1:-

define( 'MyApp', ["marionette"], function (Marionette) {

// set up the app instance

var app = new Marionette.Application();

app.on("initialize:after", function(){

console.log("initialize started.");

});

// export the app from this module

return app;

});

Example 2:-

// Fetch and execute Marionette App

require( ["MyApp"], function (app) {

// Execute App

app.start();

});

Example 3:-

define({

"root": {

"india": "india",

"australia": "australia",

"england": "england"

}

});



Discussion

No Comment Found