Difference between Require() and Define()

Define() Method:
It’s used when the js is intended to be used in another js file.
It’s used to assist in the definition of modules.
Define() method accommodates two optional parameters:
i) Module ID
ii) Dependencies : array of required dependencies

define(
  'module_id' /*optional, a string that specifies the module's name*/,
  ['dependency1', 'dependency2'] /*optional, set of array[dep1, dep2]*/,
  function(dep1, dep2) /*a callback function that defines the module.*/
    {// module code here}
);

The define() method needs to return the implementation

define(['dependency1', 'dependency2'], function (dep1, dep2) {
  //returns the implementation
  return function () {};
});

Require() Method:
The require() is used to manage the dependency load, i.e. it’s used to load modules (reusable code) into the application.
It does’t needs to return the implementation.

require(['jquery'], function ($) {
  //loads the respective dependency into the application for using it.
});

However, if you have any doubts or queries then create a ticket or drop us a mail at support@webkul.com

Thanks


Source link