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

Analysis of AMD,CMD,Commonjs instance in javascript

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this "AMD,CMD,Commonjs case Analysis in javascript" article, so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "AMD,CMD,Commonjs case Analysis in javascript" article.

1 、 AMD

AMD is the standardized output of the module definition of RequireJS in the promotion process, while the AMD specification is an asynchronous loading module, which allows callback functions to be specified.

In the AMD standard, the following two API are defined:

Require ([module], callback)

Define (id, [depends], callback)

That is, define a module through define, and then use require to load a module. In addition, require also supports module export of CommonJS.

A.jsdefine (['package/b',...], function (b) {function func1 () {b.sayHi (' hello world');} return {func1: func1}}); require (['a'], function (a) {a.func1 ()})

2 、 CMD

CMD is the standardized output of module definition in the process of promotion by SeaJS. CMD is the synchronization module definition.

/ / all modules define define through define (function (require, exports, module) {/ / introduce dependency var $= require ('jquery') through require; var C = require ('. / c.js'); exports.sayHi =. Module.exports =.})

The difference between the two is that the former is executed in advance for dependent modules, while the latter is delayed. The former favors dependence front, while the latter favors dependency proximity, that is, require only when a module is needed.

3. CommonJS specification-module.exports

Front-end browsers do not support it. This specification is used in Nodejs.

Exports.sum = function (arr b) {return a + b;} exports.count= function (arr) {return arr.length;}

The core idea of CommonJS is to load other dependent modules synchronously through the require method, and then export the interfaces to be exposed through exports or module.exports.

4 、 ES6

In ES6, we can use the import keyword to introduce the module and export the module through the exprot keyword, which is more powerful than the previous schemes, and it is also our favorite, but because ES6 cannot be executed in the browser at present, we can only compile the unsupported import into the current widely supported require through babel.

Import Home from'. / Home.vue'export default {} above is about the article "AMD,CMD,Commonjs instance Analysis in javascript". I believe everyone has a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please 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

Internet Technology

Wechat

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

12
Report