Browser-side JavaScript Loader

Source: Internet
Author: User
Tags define function

CommonJS, defines a specification for a synchronous load script. For the browser side, because the JS script is at the far end, it is possible to block threads in a synchronous way for long periods of time. Therefore, the browser side of the JS loader is not strictly according to Commonjs to do. Seajs as an attempt to follow the COMMONJS specification of the loader, is on the basis of the standard on the outside bread layer define, to asynchronously load JS, the following is an example of SEAJS official website:

 //  All modules are defined by define to  define (function   (require, exports, module) { //   var  $ = require ('  jquery '  //  provide interfaces externally via exports  exports.dosomething = ...  //  module.exports =

Define provides a function to get the module for the callback require, two objects exports and module that provide the external interface. As you can see, the dependency of a module is declared by calling require. The SEAJS will use the regular to grab the dependent module and load it. For proper gripping, SEAJS defines a number of writing specifications. After the module is defined, use Seajs.use to use the defined module.

Requirejs as a loader that complies with the AMD specification. It defines two variables, define and require, in a global context. You can define a module in the following ways:

Define (' module ', ["alphafunction (Alpha) {       2;}};});   

It can also be defined in a way similar to SEAJS. Requirejs references a module in the following ways:

Require ([' module '],function(module) {    console.log (module);});

For Seajs and Requirejs, there can be only one define function in a JS file, that is, a JS file corresponding to a module. Requirejs will execute immediately after loading a module. SEAJS will execute the module when the execution require is loaded.

There are some differences between SEAJS and Requirejs. Requirejs has shim mechanism to support those who do not meet the AMD specification of the module, SEAJS originally did not shim mechanism, and then added, the latest version has removed this feature. Requirejs support WEBWORKER,SEAJS requires third-party plug-in support.

If you need to implement a loader, the most you need to deal with is the trigger relationship between modules. If a module relies on another module, it needs to wait until the dependent module is ready and the module is in a usable state. Dependent modules may also have modules on which they depend. Therefore, a triggering mechanism is needed to allow the module to trigger the module that it depends on in the prepared state, so that it checks whether its dependent modules are ready. Seajs and Requirejs have different ways of achieving this. Here is a version of my implementation: GitHub address.

  

Browser-side JavaScript Loader

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.