Home > Web Front-end > JS Tutorial > body text

Simple parsing of javascript modularization_javascript skills

WBOY
Release: 2016-05-16 15:06:16
Original
1208 people have browsed it

This article explains the modularization of JavaScript for everyone. The specific content is as follows

AMD is the standardized output of module definition during the promotion process of RequireJS.

Load modules asynchronously, depend on them in advance, and execute them in advance.
Define definition module define(['require','foo'],function(){return});
Require loading module (pre-dependency) require(['foo','bar'],function(foo,bar){});

CMD is the standardized output of module definition during the promotion process of SeaJS.

Define definition exports export define(function(require,exports,module){}); The module stores some objects on the current module.
require(./a) is introduced directly. Require.async is introduced asynchronously.
Synchronous loading, dependencies nearby, delayed execution.

SeaJS application

Official getting started example: http://seajs.org/docs/#quick-start

How to write a SeaJS module?

// 所有模块都通过 define 来定义
define(function(require, exports, module) {
 
 // 通过 require 引入依赖
 var $ = require('jquery');
 var Spinning = require('./spinning');
 
 // 通过 exports 对外提供接口
 exports.doSomething = ...
 
 // 或者通过 module.exports 提供整个接口
 module.exports = ...
 
});
Copy after login

Load module in the page

//在 hello.html 页尾,通过 script 引入 sea.js 后,有一段配置代码:
 
// seajs 的简单配置
seajs.config({
 base: "../sea-modules/",
 alias: {
  "jquery": "jquery/jquery/1.10.1/jquery.js"
 }
})
 
// 加载入口模块
seajs.use("../static/hello/src/main")
Copy after login

The above is a brief introduction to JavaScript modularization. I hope it will be helpful for everyone to learn JavaScript modularization.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template