javascript - Difference between RequireJS and CommonJS -


I was wondering what the difference between these snippets is.

  var $ = require ('jQuery'); Var _ = is required ('underscore'); Var backbone = Required ('backbone'); (['Jquery', 'underscore', 'backbone'], function ($, _, backbone) {/ code>  

and

  // code   

-content "itemprop =" text ">

most Consider the snippet first, which is in common JS style:

  var $ = required ('jquery'); var _ = is required ('underscore'); Var backbone = Required ('backbone');   

These calls are synchronous calls: When the requirement returns, it returns the module you requested. CommonJS Required There is a proposal to support asynchronous forms of requirement , but as far as I have not progressed beyond the resolution level, the code is Node.js to Is required.Aynic which has been deleted, is one that applies it though. This package looks like using AMD style modules.

Now, Consider the second snippet, which is in AMD style:

  Required (['jquery', 'underscore', 'backbone'], function ($, _, backbone) {// code goes here})   

Since requirements require AMD type Modules implement the system, with the above code works, RequireJS is required for this call - as suggested by the asynchronous module definition (AMD) --- asynchronous. You can not rely on the return value of the requirement to get the module value. You have to use a callback instead. The defined call works in a similar way, but defines a modules other than the module's requirement.

Now, if you use RequireJS, then it provides those features, that when you define the module so that you can define a module like this:

  Define (['jquery', 'underscore', 'backbone'], function ($, _, backbone) {// code goes here});   

Or use something that looks like a common JS idiom:

  define (function (requirement) {var $ = required (' Jquery '); var _ = is required (' underscore '); var backbone = is required (' backbone '); // code goes here});   

It's really easy to change a CommonJS style module to use with RequireJS: simply wrap it with the defined call One is to help in the conversion.

Behind the scenario, the RequireJS reads the code of the callback in another form and creates a list of dependencies so that it can be interpreted in the end as:

  define ('Need', 'jquery', 'underscore', 'backbone'], function (requirement) {var $ = requiring ('jquery'); var _ = ('underscore'); var BackBone = Required ('backbone'); // code goes here})   

This can be amazing (seeing that AMD is asynchronous) that the requirement Call callback Synchronous are part of the RequireJS support for the CommonJS genre. RequireJS supports a kind of synchronous requirement call but with the following warning: If the module already To the synchronous requirement , the value of the synchronous requirement gives the value of the module, but otherwise it immediately fails To load this module, Q Do it does not. Because RequireJS interprets a module definition that uses the CommonJS style as I have shown above - such that the dependency is actually listed in the defined arguments - so these modules are guaranteed to be loaded Is synchronous call is done for the time the is required .

In addition to being able to use the CommonJS module in the RequireJS (a cover is provided), it is possible to use the modules designed for it as a normal JS environment like Node.js. For example, I have used the module to load which I have designed as an AMD module in node.js.

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -