Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of js Modularization Specification

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the example analysis of js modularization specification, which is very detailed and has certain reference value. Friends who are interested must finish it!

1. CommonJS

For server-side modular programming, for example, nodejs adopts this specification

A file is a module. The require method is used to load the module. This method reads a file and executes it, and finally returns the module.exports object inside the file.

Require reads .js files by default, so require (module name) can not write a suffix.

Load synchronously, because the modules loaded on the server side are generally local, so this can be done; but on the client side, if a module is too large, it will lead to a "fake death" of the page.

The module.exports attribute indicates the output interface of the current module. Other files loading the module are actually reading module.exports variables. For convenience, you can also use exports,exports to point to module.exports;, that is, exports = module.exports = {}.

Exports.xxx is equivalent to adding an attribute to the exported object, which is visible to the calling module

Exports = is equivalent to reassigning exports, thus severing the association with module.exports, and the calling module cannot access the object and its properties of exports

2. AMD

Load module: require ([module], function (module) {})

Define module: define ([module], function (module) {}); module is dependent module

Require.js (the tool library of front-end modular management) realizes the asynchronous loading of js files to avoid loss of response of web pages; manages the dependence between modules to facilitate code writing and maintenance.

Depending on the front, the execution module depends on the block as soon as possible, and the execution order is not necessarily 1 followed by 2.

Load non-standard modules

Require.config ({baseUrl: "js/lib", paths: {"jquery": "jquery.min", "underscore": "underscore.min", "backbone": "backbone.min"}, shim: {'underscore': {exports:' _'}, 'backbone': {deps: [' underscore', 'jquery'], exports:' Backbone'}) / / exports value (the name of the output variable), indicating the name of the external call to this module Deps array, indicating the dependency of the module

3. CMD

Define (function (require, exports, module) {var a = require ('a'); a.foo ();}

Sea.js

Rely on the nearest, and execute the dependency module only when it is really needed, in a fixed order

The biggest difference between AMD and CMD is that the timing of execution of dependent modules is different, rather than the timing or mode of loading. Both of them are asynchronous loading modules.

AMD depends on the front, and js can easily know who the dependent module is and load it immediately, while CMD depends on it nearby and needs to parse the module into a string to know which module it depends on.

The above is all the content of the article "sample Analysis of js Modularization Specification". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report