Modular Programming (ii)---common.js

Source: Internet
Author: User

COMMONJS is the specification of server-side modules, which are used by node for promotion. Because of the complexity of server-side programming, it is difficult to interact with the operating system and other applications without modules. Here's how to use it:

According to the COMMONJS specification:

    • A separate file is a module. Each module is a separate scope, meaning that variables defined inside the module cannot be read by other modules unless they are defined as global properties of the object.

    • The best way to output module variables is to use module.exports objects.

    • The load module uses require a method that reads a file and executes it, returning an object inside the file module.exports

Look closely at the code above and you'll notice that require it's synchronized. The module system needs to read the module file contents synchronously and compile execution to get the module interface.
However, there are a lot of problems with the browser side.

On the browser side, the best and easiest way to load JavaScript is to document insert <script> tags in. But script tags are inherently asynchronous, and traditional CommonJS modules do not load properly in a browser environment.

One of the solutions is to develop a server-side component that makes static analysis of the module code and returns the module to the browser side, along with its dependency list. This works well, but requires the server to install additional components and therefore adjusts a series of underlying architectures.

Another approach is to encapsulate the module definition with a standard set of templates:

On the browser side, the best and easiest way to load JavaScript is to document insert <script> tags in. But script tags are inherently asynchronous, and traditional CommonJS modules do not load properly in a browser environment.

One of the solutions is to develop a server-side component that makes static analysis of the module code and returns the module to the browser side, along with its dependency list. This works well, but requires the server to install additional components and therefore adjusts a series of underlying architectures.

Another approach is to encapsulate the module definition with a standard set of templates:

The template code provides the opportunity for the module loader to perform static analysis of the module code and dynamically generate dependency lists before the module code executes.

turn from Https://www.jianshu.com/p/d67bc79976e6 front-end modularity (COMMONJS,AMD and cmd)

Modular Programming (ii)---common.js

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.