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

How to use AMD,CMD,Commonjs in javascript

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

Share

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

In this article, the editor introduces in detail "how to use AMD,CMD,Commonjs in javascript". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use AMD,CMD,Commonjs in javascript" can help you solve your doubts.

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 {} read here, this article "how to use AMD,CMD,Commonjs in javascript" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, 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

Internet Technology

Wechat

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

12
Report