Sei sulla pagina 1di 3

Contents

Background:............................................................................................................... 1
AMD(Asynchronous Module Definition):......................................................................1
Common.js :............................................................................................................... 1
Require.js:................................................................................................................... 1
Conclusion:................................................................................................................. 2

Require.js And Common.js


Background:
Require.js and common.js both are JavaScript frameworks, which
are used to write modular code in JavaScript. When we say modular its composed
of a set of highly decoupled, distinct pieces of functionality stored in modules. Using
this we make our code loose coupled which facilitates easier maintainability of apps
by removing dependencies where possible. When these are implemented efficiently,
its quite easy to see how changes to one part of system may affect another.

AMD(Asynchronous Module Definition):


AMD is a JavaScript specification that defines an API for defining
code modules and their dependencies , and loading them asynchronously if
desired.So we can load files only when ever they are required. If we want to load
files asynchronously then we can choose AMD in our project.It provides following
benefits :

Simple, flexible module format.


Forward compatibility w/ ES.Next / Harmony modules (via translation)
Designed with browser envireonment in mind.
Designed to accomadate asynchronous loading.
Plugins for loading other resource types.
It's possible to lazy load scripts if this is needed.

Common.js :
The CommonJS group defined a module format to solve
JavaScript scope issues by making sure each module is executed in its own
namespace. This is achieved by forcing modules to explicitly export those
variables it wants to expose to the universe, and also by defining those
other modules required to properly work.It uses require, module, and exports
free variables, i.e. they're implicit.Benefits provided by Common js are:

It is synchronous in nature.
Best suited for server side development as it is synchronous in nature.

Require.js:
RequireJS is a JavaScript file and module loader. It is optimized for inbrowser use, but it can be used in other JavaScript environments, like Rhino

and Node. Using a modular script loader like RequireJS will improve the speed and
quality of your code.Requirejs provides following benefits:

RequireJS is an implementation of AMD, while at the same time trying


to keep the spirit ofCommonJS (mainly in the module identifiers).
RequireJS, while being an AMD implementation, offers a CommonJS
wrapper so CommonJS modules can almost directly be imported into
use with RequireJS.

Conclusion:
AMD gives us more benefits when we want to load the files through
asynchronous calls and commonjs is preferred if we want to make synchronous
calls. As Require.js is a implementation of AMD ,it is well suited for web applications
if we are going to use AMD in our application and also it provides part of commonjs
with a wrapper.

Potrebbero piacerti anche